Hi all, loving Serverless Stack!! Does anyone know...
# help
n
Hi all, loving Serverless Stack!! Does anyone know if it's possible to spin up a StaticSite resource from a Lambda? Basically the Lambda would take in some text and then create a StaticSite with that text in the html.
o
Umm maybe have the lambda commit a new branch to git, and have seed deploy it automatically? Or have the lambda run a bash script to either directly deploy, or call out to a CI?
n
Good thinking but I feel like having many git branches would get messy with, say, 50 static sites. At the very least, I feel like there should be a way to launch a cloudformation stack given a template. But ideally I could keep things within sst. Any ideas there?
o
You could call SST programatically, although I don’t think that’s a supported API so you’d have to pin the version. There’s a deploy function you could call, pass it the right config to a folder that you’ve created with the stack to be deployed
n
Ah, I'll look into this. Where did you find this deploy function? Can't seem to find docs
o
Personally if I was doing this I’d rather like having a copy of each thing that I deployed stored in git. Then if I needed up update anything or update all deployed stacks, I can iterate through branches and making the appropriate changes. 50 branches is not that many, I’d think of git as my database.
Yeah you’ll have to go digging through the codebase
n
Ok, I'll give the git approach a shot. Thanks for the help!!
n
Thanks--I also found this code for boto3:
Copy code
import boto3
cf_client = boto3.client('cloudformation')
cf_client.create_stack(
    StackName='your-stack',
    TemplateURL='<https://s3.amazonaws.com/your-bucket/your-template>'
)
Do you think something like this could work?
o
You can try taking the output from
sst build
reading the template from the .build folder and seeing if it deploys. But like the deploy option above, you’d have to figure out how to call it programmatically, if you’re doing that, you may as well let let SST/CDK handle the deployment too instead of just the build
n
Wait but wouldn't the boto3 approach work if I had a template url? I could just include that code in the lambda?
o
Not sure what you’re doing but yeah if the template is the same every time you can point to it. Or upload it to S3 then point to it
and I don’t know how that template references the actual content to deploy, never used StaticSite myself
n
Ok, well I can look into that deploy script too and I'll keep this thread updated. Thanks again for the help 🙏
t
Is this for sites that are user generated? I can be tempting to use IaC for user generated logic but I think it's better written in application logic
n
Yes, think how Shopify generates user stores. How would this be done with application logic?
f
Another thought similar to what @Omi Chowdhury suggested.. what if ur
lib/index.js
looked like this:
Copy code
new StaticSiteStack(this, "site1");
new StaticSiteStack(this, "site2");
...
new StaticSiteStack(this, "site50");
and ur Lambda adds a new stack to the app and commits it?
d
@Nikolas Ioannou Is it a requirement that all sites are managed in the same stack? Seems to me like you would build a defacto CFN template that is your golden setup for a site, any items you want to be dynamic you pass those in as params into the template. Then it’s a simple, host the template in S3 and use SDK to create N number of stack/sites with that template. • Reuse templates • Site + Stack separation • No specific IaC dependency. Use what you love to generate template. If that sounds applicable, I show how to do this with raw CFN in the Mastering CloudFormation Course - Chapter 8 over at ACG.
n
@Frank by "Lambda adds a new stack to the app and commits it" do you mean adding:
Copy code
new StaticSiteStack(this, "siteXX");
to
index.js
and committing that? I didn't do a great job indicating the specifics but I feel like this would get messy if this grew to hundreds of sites.
t
Yeah if you do want to use IaC to manage user generated things, I'd suggest doing a stack per customer. You can use SST/CDK still and just do
sst build
to generate the CF template that can be used
I would suggest avoiding the gitops workflow for something like this
n
@Dan Van Brunt @thdxr I think I understand. So I would use
sst build
to get the template for my "defacto CFN template" which I can pass in params to. Host this template in S3 and then use the SDK's
createStack
to create a number of those on command?
t
Well you can't exactly do that if the variables per customer effect the staticsite's build
n
@thdxr new to this but are you saying I can't pass in params to the template to change, say, text or images on the static site per customer?
t
Right because the build would have already happened
There's a lot of different ways to do this so hard to synthesize this into a recommendation because it depends what constraints you have. You can try the following approach: In index.ts for your entry point to sst
Copy code
export default async function main(app: <http://sst.App|sst.App>): void {
  const sites = fetch_from_api()
  // loop through sites and initiate one stack per site
}
The downside of this is you need to retrigger a CI build whenever a user adds a new site Another approach is: In your application define a stack, write it to file, and call
sst deploy
dynamically on it. Right now there isn't an easier path to programmatically call sst
Overall what you're trying to do is fairly advanced so it's going to be a lot of work to get it going. AWS even has an entire service dedicated to this workflow (can't remember the name atm)