This demonstration is to show how we can start using CDK Constructs and enhance the producticty and share our published constructs to other team members for the benefit of DevOps Principles.
Here, the reader is playing two roles (Package Developer and Application Developer) in order to undestand how to implement this solution.
Role of the shared services developer is to develop services which are common across the originazation like CodeArtifact etc. that can be consumed by other teams.
Role of the package developer is to develop a CDK Construct, in our case its going to be a re-usable construct for building a Codepipeline for lambda function that has following functionalities:
- Builds the docker file
- Push the docker file to ECR (Container Registry)
- Pull the docker file from ECR
- Update Lambda function with the latest docker image.
Once the consctruct is ready for publishing, Pacakge Developer is going to publish this lambda-pipeline-construct
as a npm
package to AWS CodeArtifact (Its a artifact repository, just like Artifactory, nexus or jfrog)
Role of the Application Developer is to include that lambda-pipeline-construct
in package.json
file and feed project specific variables like coderepo, branch, etc and does cdk deploy, thats it!!, Application developer would be seeing a Codepipeline specific for his lambda application.
- Install Nodejs
- Install Docker
- Install VS Code (Optional)
- Install AWS CDK (latest version)
- Install AWS CLI
- Configure AWS CLI with AWS Account (do
aws sts get-caller-identity
for validation)
cd prerequisites/codeartifact
- Create CodeArtifact Repo, when you want to publish this
lambda-pipeline-construct
package- (Optional) make necessary changes to
config/config.json
file - run
npm install
- run
cdk synth
review template file fromcdk.out
folder. - run
cdk deploy
- you should be seeing an output something like this:
take note of✅ CodeArtifactResources ✨ Deployment time: 57.14s Outputs: CodeArtifactResources.amznapp101coderartifactdomainname = amzn-app1-01-cdk-construct-demo CodeArtifactResources.amznapp101coderartifactreponame = amzn-app1-01-lambda-pipeline Stack ARN: arn:aws:cloudformation:us-east-1::stack/CodeArtifactResources/297bfb30-4410-11ed-bd03-0ec07ab1c2b3
repo-name
anddomain-name
6. If you are seeing error related tobootstrap
, run thiscdk bootstrap
command and re-trycdk deploy
- (Optional) make necessary changes to
cd ../../package-developer/lambda-pipeline-construct/
- Replace Appropriate domain and repository in the following command and run it in
terminal
aws codeartifact login \ --tool npm \ --domain <replace-with-domain-name> \ --domain-owner $(aws sts get-caller-identity --output text --query 'Account') \ --repository <replace-with-repo-name>
- You should be seeing an output simillar to this:
- run
npm install
- Build a npm pacakges by
npm run build
- Publish construct with the following command
npm publish
- You should be seeing an output simillar to this:
- Congratulations !! you made it!!
- Now you can validate it by going to AWS Console in CodeArtifact service.
Note: If you run into this error
npm ERR! Unable to authenticate, your authentication token seems to be invalid.
npm ERR! To correct this please trying logging in again with:
then delete package-lock.json file
cd ../../application-developer/team-a-lambda-pipeline/
- Open
package.json
reviewdependencies
section"lambda-pipeline-construct": "1.0.0"
if it doesnt exist please refer toProvision Infrastructure - Package Developer
steps and update the package name. - run
npm install
- run
cdk synth
review template file fromcdk.out
folder. - run
cdk deploy
- you should be able to see the output simillar to this:
- Congratulations !! you made it !!.
- Now go to Codepipeline from AWS Console and you should be seeing a pipeline that has been created.
- Now, if you want to create a new pipeline for new team, all you need to do is copy
team-a-lambda-pipeline
folder and make necessary changes inconfig/config.json
file and runnpm install
andcdk deploy
. Thats it!!
Note: If you run into this error
npm ERR! Unable to authenticate, your authentication token seems to be invalid.
npm ERR! To correct this please trying logging in again with:
then delete package-lock.json file
Finally, dont forget to delete the resources by cdk destroy
command from terminal.
Reference urls:
https://aws.amazon.com/about-aws/whats-new/2022/04/aws-lambda-function-urls-built-in-https-endpoints/
https://aws.amazon.com/blogs/aws/announcing-aws-lambda-function-urls-built-in-https-endpoints-for-single-function-microservices/
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda-readme.html#function-url
https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html#getting_started_install
https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.