How can I create CustomResource in SST framework. ...
# help
p
How can I create CustomResource in SST framework. Here is what I am trying to achieve. CustomResource creation will trigger lambda. That lambda looks up db and create the NextJsWebsite (of cource by cloning the somegit repo)
Copy code
// In the stack

new CustomeResource(function: sst.Function);
Copy code
// In the lambda

const handler = (event) => {
  if (event.RequestType === 'create') {
    // do something like, get websites metadata from db and for each websites deploy.
    new NextJsWebsite(); // sst resource
  }
}
d
Can you explain a bit more about why you are thinking to
CFN -> Custom Resource -> DB lookup -> Website
? and not something like
DB Stream -> Lambda -> CFN -> Website
Usually you use CRs where there isn’t already CFN resources to do the same. You don’t want to duplicate resources in lambda code that can better be handled by CFN templates directly.
At first glance your proposed setup seems like a CFN antipattern.
p
@Dan Van Brunt I need to dynamically creating n number of website when ever code change (CI/CD). Next, when ever new record is added to database a new website needs to be created. So in in both scenario (CI/CD and Db update) I need to create/update website. So, I want to put the common code to create/update website in lambda.
So the plan is in CI/CD scenario this lambda is triggered via CNF customResource. In DB update scenario this lambda is triggered using Stream event.
d
hmmm… CFN should map tightly to resources in AWS. If you have two ways for the same resources to be created… it feels like a pretty strong antipattern.
p
Should CFN map tightly to AWS resource? Isn't the whole point of having CustomResource is to create resource which cannot be mapped to AWS resource?
d
I’m also not sure, why both need to happen… if every site gets added to the DB then they will all be created… also… you would not have the DB Stream manually create the resources…. you could have it kick off your CI/CD pipe which in turn creates the site…. or have it use
aws-sdk.cloudformation
to deploy your prefabbed (these can be source controlled and created with SST) CFN templates
https://serverless-stack.slack.com/archives/C01JG3B20RY/p1642514850180800?thread_ts=1642511271.179700&cid=C01JG3B20RY Yes and no. In my understanding … CFN should map tightly to resources 1:1 “when it can”. There are some resources not supported by CFN (AWS Transcoder Pipe) or scenerio’s that aren’t supported (XCross-Region buckets or external service “Algolia Index”) those are cases where you would use a CR. Another case would be to use it to handle a process. One example of this would be the CDK’s autoDeleteObjects property in their Bucket construct. Under the covers this creates a CR that the Bucket resource depends on… when the stack is deleted…. the CR emptys the bucket of all objects (something that would otherwise fail on stack delete) so that the stack can be deleted without error.
p
Let me put the above scenario in different sentence. Imagine there is custom CI/CD which takes care of the deployment of n website, just like (Algolia Index). On every CNF deployment (sst deploy) I need to trigger this custom CI/CD (just like triggering Algolia Index). I think this can be done using CustomResource. So when
sst deploy
happens I trigger
Create n website
when
sst remove
happens I trigger
Remove n website
d
This is the part that seems odd to me
Code Change > CI > CFN -> CR/Lambda -> Deploy Website
first… can you clarify what you see being done specifically in that
CR -> Deploy Website
step?
p
It's not like this
Code Change > CI > CFN -> CR/Lambda -> Deploy Website
It's more like this
Code Change > CR > Lambda -> CDK -> Deploy Website
d
ya that doesn’t make sense to me.
First lets not confuse things with SST or CDK…. at the end of the day those are just CFN templates
p
Could you elaborate like why and how it can be solved in other way?
d
it doesn’t make sense to me to do this…. EVER
CFN template -> CR/lambda -> CFN template
this begs the question…. why would you not just ….
CFN template -> website
https://serverless-stack.slack.com/archives/C01JG3B20RY/p1642519416183200?thread_ts=1642511271.179700&cid=C01JG3B20RY It’s tough since I don’t really understand what your trying to do. I just know that this
CFN template -> CR/lambda -> CFN template
doesn’t make sense to me in any context.
p
Imagine you have an admin website, with a form where you enter subdomain name and other details. And hit submit. Now this should trigger deployment of website. So lets say this happens like this.
Submit -> Create DB record -> Lambda/CNF create website
Would the above part sounds good?
d
CR’s map to “resources” (Algolia index) or “processes” (empty a bucket) and never to AWS resources and certainly not to a lambda who’s job it is to deploy another CFN template of resources. The only caveats there is when CFN doesn’t support a given AWS service/resource or structure. So a qualifying question might be…. “Can I build this thing into my root CFN template?” if the answer is yes… it makes almost no sense to add it behind a CR in your root template.
I think I know what you are trying to build now. Is it a process by which someone can “Create a deployment/site” from an admin panel…. then later add the frontend assets to that site? Essentially you are looking to build something like Netlify?
p
Exactly, someone is able to create website from a form submit. So it happens like this. 1. User submits the form. 2. Lambda gets triggered 3. Lambda downloads code from git repo and creates resources (cloudfront, s3, route53, etc) 4. Site is deployed. Now the question how do I handle when the code in
git repo
change? Can I reuse the Lambda from above flow to redeploy to all the sites?
That's where I am thinking of CR, which triggers existing Lambda from the above flow.
d
yes… in this case (Netlify) there are two separate parts • the infra (CFN template, dns, distro, buckets, etc) • the website code (github repo with ZERO infra in it)
you have a single source of truth for both those things
the user brings their own website repo… and netlify has a repo/code behind the scenes that represents the infra
of course, some of their platform is likely to be highly dynamic and thus likely not managed by CFN. Eg. dns, distro etc.
in those cases…. a users changes to the admin form would likely net out to lambda being called that makes direct AWS SDK calls to update that user’s distro, hostedzone etc.
its not that you couldn’t do that part with CFN….. but I wouldn’t think it would be worth it.
happy to chat over a hangout / zoom to give you more details.
p
We can huddle on slack, if you are free now.
d
I noodled what it would take to build something like netlify myself as well.