Code Pipeline Using the AWS CDK for Lambda

In my last post where I have tested my lambda function using AWS SAM locally. Here, I will do the deployment on my lambda application via AWS CDK. I have made some changes for deployment, as I have not used SAM template and added package.json file and had stored my code with the CDK application.

I am basically using this example from AWS CDK.

https://docs.aws.amazon.com/cdk/latest/guide/codepipeline_example.html 


Some prerequisite, on this before beginning ahead.

I have installed below mention packages in my directory named lambdapipeline.

My project type for CDK is Type Script. If you wanted to know how to get started with typescript with CDK please visit my earlier blog post on Using CDK with Type Script as  a language type, in which I have created a S3 bucket using CDK.

Once, I have created my basic step-up for my directory structure by running

cdk init --language typescript


@aws-cdk/aws-lambda
@aws-cdk/aws-codedeploy
@aws-cdk/aws-codebuild
@aws-cdk/aws-codecommit
@aws-cdk/aws-codepipeline
@aws-cdk/aws-codepipeline-actions
@aws-cdk/aws-s3

using npm install and the package names above.

I am also using aws CDK Version: 1.3.0 (build bba9914).

I have share my code in my github repository.

https://github.com/nikhilbhoj/lambdacicd 

This has full lambda code structure and CDK application code.

I will now show the steps that I have followed for Lambda deployment


Step 1.

My directory structure



Step 2.

My Lambda code and package.json file which I had used in earlier blog post, it is simple typescript file.
Also, when I do cdk ls you can notice, I have two stacks. I will deploy only PipelineDeployingLambdaStack.



Step 3. My Github repository for this blog post.


Step 4.

Then I complie my application using npm run build and use cdk deploy       PipelineDeployingLambdaStack to deploy the  pipelinestack that will create pipeline, codebuild and pipeline actions.

Please note that I have already created a repo name lambdarepo and I have hard coded this in my code.

You can do the same or by programmatic  create a codecommit repo and pass on that information to AWS code pipeline.




Pipeline is deployed successfully, it has created lots of AWS resources.

Step 5.

Initially my lambdarepo  repository will not have any code, so my pipeline will fail.



Step 6.

Once I have pushed my code which is the lambdapipeline directory where I am working and from which I have created this pipeline into lambadrepo like this below.


Step 7.

My Pipeline will start working and all the steps will go green one by one.





Step 8. 

Verifying it in the Cloud formation console and Lambda console.




Step 9.

Verifying the Lambda code too.



Step 10.
Now, it is time to delete everything. Here order is important.

Then deleting the Lambda Stack from cloud formation console.



Step 11. 

Now, deleting the Pipeline CDK stack via command prompt and verifying it in cloud formation console.




Comments

  1. Hi,
    I am getting the following error in lambda_build stage

    [Container] 2020/02/07 00:33:21 Expanding index.js
    [Container] 2020/02/07 00:33:21 Skipping invalid file path index.js
    [Container] 2020/02/07 00:33:21 Expanding node_modules/**/*
    [Container] 2020/02/07 00:33:21 Phase complete: UPLOAD_ARTIFACTS State: FAILED
    [Container] 2020/02/07 00:33:21 Phase context status code: CLIENT_ERROR Message: no matching artifact paths found

    ReplyDelete
  2. Can you share your project structure ? Also, do not check-in node_module by excluding it in the .gitignore file.

    ReplyDelete

Post a Comment

Popular Posts