Bitbucket Pipeline build and deploy Lambda Container Image in AWS
Tags: bitbucket, aws, devops, serverless
Published: 2022-07-12 09:10:19

**Summary**
In this post, I want to share how I build CI/CD system that Bibucket Pipeline can build and deploy Lambda Container Image in AWS.
The architecture of this post:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ne0z2erlm1ksu7lbb9x.png)

**Steps:**
Step 1: Create a repository at Elastic Container Registry
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjl5neu1gnt2x5nxxg1z.png) 
I set it as private repository, Linux Operating systems, Architecture x86
After create the ECR repository, we can see the image URI and the push command to know how we upload our container to ECR.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7poz7zcalg9kun8vriu.png) 
The push commands:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ycsvrk7qfv2cier0dlhu.png)
 
Step 2: Push a container from your local machine to ECR.
Currently, I use sample python lambda docker from https://github.com/lambci/docker-lambda.git
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6gc26pissjs5hx3gx6ot.png)
 
Step 3: Create Lambda Container Image that using the Image URI
Choose the container tab
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ub8khrjbxe7grasxchpv.png) 
Choose the repository and the container we've just pushed
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t44jkw1hqx3hvv2mi59u.png)
 
Step 4: Create Bitbucket repository
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05mb53e45e778p9o97rb.png)

Step 5: Add bitbucket-pipelines.yml file to the projects to run pipeline and push it to Bitbucket Pipeline
We need to fill some information from AWS to bitbucket-pipelines.yml like IMAGE_NAME, FUNCTION_NAME, the link in "docker login"
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wga9er45ljzlhmtosq7a.png)

Step 6: Set up your AWS credentials in repository's variable
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x5pfo2hr0cbf9rvezu6s.png)
 
  
Step 7: Run the pipeline in Bitbucket and wait for the pipeline completes build Lambda container images, push to ECR and Update Lambda Image URI.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouepohwx7zr4k63m29sd.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5wartm826f42gjqnxa7.png)
Step 8: Check the image we pushed in ECR from Bitbucket pipeline, check the Lambda's Image URI with the name convention: TAG=$BITBUCKET_BUILD_NUMBER-$BITBUCKET_BRANCH-$BITBUCKET_TAG
In my case, the BITBUCKET_BUILD_NUMBER = 2 and run in master branch with empty BITBUCKET_TAG
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ieiheguhcon7uh4piw0y.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/er5svxwbigrygqdqfnf5.png)

That's all of what I want to share in this post.
Please let me know if you have other questions!