Simple CI/CD with Bitbucket Pipeline and AWS
If you have a repository in bitbucket and you want to have a CI/CD process within your Bitbucket and AWS, you can build it by using Bitbucket Pipeline.
Bitbucket pipeline is a simple CI/CD pipeline, you can use AWS S3 to store the artifact from Bitbucket and deploy it to EC2, ECS or lambda with AWS Code deploy.
To create a simple CI/CD, you can follow these steps:
- Prepare your bitbucket repository. To store your code and keep your code fully integrate.
- Create variable repository in your bitbucket repository. It is required to keep your AWS credential not placed in your repository.
- Then you need to create bitbucket-pipelines.yml file. The files contain how your pipeline will work. In this example, I need to run manually my pipeline by triggering an input before the pipeline running. Please see my pipeline below.
There it is, my bitbucket pipeline. So that YAML file will run the pipeline as below.
- First step it will create a zip file, the zip file will contain index.html and appsec.yml file. The appspec file is needed for AWS codedeploy running the deployment. This is the example of appspec.yml.
- The second step, it will upload the zip file that we created in the first step to our s3 bucket. You need to specify your S3 bucket and you may also need to define AWS secret access key as repository variable to keep your AWS key secure.
- The last step will create a deployment in specific application and deployment group in AWS Code Deploy, like upload process you need to define your AWS credential(AWS access key and Secret Access Key). Also, you need to specify the application and deployment group. Don’t forget to specify the s3 bucket as deployment sources.
- WAIT: ‘true’I set the bitbucket pipeline to wait deployment until it finish, after that bitbucket also detected as successfully deployment.
That’s it, just a simple way to create a pipeline in Bitbucket. Next step we will go to AWS console to setup S3 bucket and Code Deploy.
Create S3 Bucket
- Go to AWS Console
- Click Services and search S3
- Once you in S3 homepage, you may create new bucket.
- Then give your bucket name and select your bucket region(in this case I will create the bucket in Singapore region)
- After that just click “create” let the rest configuration as default
Create and setup Code Deploy
- Go to AWS Console
- Click Services and search CodeDeploy
- Under Deploy Menu, click applications
- Click “Create application”
- Give your application name and choose the platform as EC2/On-premises
- After your created an application, you need to create a deployment group. Click your application and click button “Create deployment group”
- Fill all the required field when you create deployment group. You can set like this.
If you dont have a service role to run your Code Deploy, you need to create it first.
{
"Version": "2012–10–17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": [
"codedeploy.us-east-2.amazonaws.com",
"codedeploy.us-east-1.amazonaws.com",
"codedeploy.us-west-1.amazonaws.com",
"codedeploy.us-west-2.amazonaws.com",
"codedeploy.eu-west-3.amazonaws.com",
"codedeploy.ca-central-1.amazonaws.com",
"codedeploy.eu-west-1.amazonaws.com",
"codedeploy.eu-west-2.amazonaws.com",
"codedeploy.eu-central-1.amazonaws.com",
"codedeploy.ap-east-1.amazonaws.com",
"codedeploy.ap-northeast-1.amazonaws.com",
"codedeploy.ap-northeast-2.amazonaws.com",
"codedeploy.ap-southeast-1.amazonaws.com",
"codedeploy.ap-southeast-2.amazonaws.com",
"codedeploy.ap-south-1.amazonaws.com",
"codedeploy.sa-east-1.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
And edit the trust relationship like this.
{
"Version": "2012–10–17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": [
"codedeploy.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
- After that open your application and create Deployment Group.
- You can follow the below setting for the deployment group.
Keep the other as default settings.
*p.s: Make sure your code deploy agent running on the instance.