Azeem Akhter
Azeem Akhter

Reputation: 567

CDK: Deploy microservice endpoints (lambdas) individually

I am writing IAAC with CDK for a microservice that'll be using APIGateway's RestAPI.

I have one stack with all the lambdas and the restApi in one place, I can deploy everything just fine.

Now the problem is when two people are working on different endpoints I would want them to be able to deploy just the endpoint(lambda) they are working on. Currently, when they deploy, CDK deploys all the endpoint from their repo overwriting changes someone might have deployed from their branch.

I would happily share some code but I am not really sure what to share. I think I can use some help with how to structure the code in stacks to achieve what I need.

Upvotes: 1

Views: 1301

Answers (4)

Kcrik K
Kcrik K

Reputation: 51

One idea as well is for the developer to deploy the pipeline with their code branch name, so they can have a fully fledge environment without worrying about overriding the other developer's lambdas.

Once they're done with the feature, they just merge their code in the main branch and destroy their own pipeline.

It's a common approach :-)

Upvotes: 1

Azeem Akhter
Azeem Akhter

Reputation: 567

Thanks for your input guys. I have taken the following approach which works for me:

const testLambda = new TestLambda(app, "dev-test-lambda", {
  ...backendProps.dev,
  dynamoDbTable: docStoreDev.store,
});

const restApiDev = new RestApiStack(app, "dev-RestApi", {
  ...backendProps.dev,
  hostedZone: hostedZones.test,
  testFunction: testLambda.endpointFunction,
});

Now if a developer just wants to deploy their lambda, they will just deploy the stack for the lambda which won't deploy anything else. Since the restApiStack requires lambda as a dependency, deploying that will also deploy all the other lambdas all at the same time.

Upvotes: 0

Hussain Mansoor
Hussain Mansoor

Reputation: 3124

You have 2 options to solve this problem without much work.

First one is to use code to identify who is deploying the stack. If developer 1 is deploying the stack then set some environment variable or parameter to stack. Based on that value, CDK code should compile only 1 of the endpoint repos.

Second option is to not build the repos as part of (CDK) deployment. Use Continuous Delivery (or anything else) which builds the repo code separately and CDK only deploys them.

Based on the context of your project any one strategy should work fine for you. Or share more context if it's not covered until now.

Upvotes: 1

Balu Vyamajala
Balu Vyamajala

Reputation: 10333

You have one api gateway shared across two different endpoints from two different repos.

There are couple of ways that I can think of:

Option 1: we need 4 stacks.

  • Gateway Stack: Api Gateway and Root endpoints.
  • Endpoint1 stack: Lambda and necessary routes.
  • Endpoint2 stack: Lambda and necessary routes.
  • Gateway Deploy stack: Deploy the stage.

Each time a lambda function is changed, deploy its own stack and the deploy stack.

Option 2: we just need 1 stack but deploy lambdas separately.

  • Single CDK project which deploys everything. Only thing to keep in mind is artifacts for the lambda functions should be taken from S3 bucket location.
  • Within individual pipelines of each lambda, copy artifacts to same S3 location referenced by lambda in cdk and trigger an update to lambda with a aws cli update-function-configuration as simple of update description with a timestamp or an env variable, just to trigger a new deployment.
  • This way either we can seamlessly deploy CDK pipeline or individual lambda pipeline

Upvotes: 1

Related Questions