Last active
January 23, 2022 20:02
-
-
Save 4lessandrodev/9ab5301c29112cc5df941bfb90b31527 to your computer and use it in GitHub Desktop.
deploy to ec2 from bitbucket using aws codepipeline and codedeploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Create S3 bucket | |
> name: CodeDeployBucket | |
--------------------------------------------- | |
Create IAM role for EC2 | |
> name: EC2CodeDeployRole | |
> policy: AmazonEC2RoleforAWSCodeDeploy | |
> set key: | |
> Name: EC2CodeDeployRole | |
> attach policy: AmazonS3FullAccess | |
--------------------------------------------- | |
Create IAM role for CodeDeploy | |
> name: CodeDeployRole | |
> policy: AWSCodeDeployRole | |
> set key: | |
> Name: CodeDeployRole | |
--------------------------------------------- | |
Create new EC2 instance | |
> Select IAM role EC2CodeDeployRole | |
> Create a tag for instance: Name: EC2CodeDeployEC2 | |
> Set name for security group: EC2CodeDeploySG | |
> Set port 80 accessible for all ip | |
> Add follow command on User data as text: | |
> Or alternatively you can connect EC2 instance by SSH | |
> Execute each command | |
# -------------- BEGIN -------------- | |
#!/bin/bash | |
sudo apt-get update -y | |
sudo apt-get upgrade -y | |
sudo apt install ruby-full -y | |
sudo apt install wget -y | |
cd /home/ubuntu | |
wget https://aws-codedeploy-us-east-2.s3.us-east-2.amazonaws.com/latest/install | |
sudo chmod +x ./install | |
sudo ./install auto | |
sudo apt install python-pip -y | |
sudo pip install awscli | |
# -------------- END -------------- | |
> Check if all was installed with success | |
> sudo service codedeploy-agent status | |
> If error: No AWS CodeDeploy agent running | |
> run the command: sudo service codedeploy-agent start | |
> check if its running: sudo service codedeploy-agent status | |
--------------------------------------------- | |
Access CodeDeploy console | |
> Click on Applications | |
> Create application | |
> Select Compute plataform: EC2/On-promises | |
> Create deployment group | |
> Set a name: "my-app-example" | |
> Select service role: CodeDeployRole | |
> Set deployment type: in-place | |
> Select Environment configuration: Amazon EC2 instances | |
> Select by key: | |
> start a new Custom Deployment | |
> search for EC2 instance by Name: EC2CodeDeployEC2 | |
> Set deployment settings: CodeDeployDefaultOneAtTime | |
> Disable loadbalancer | |
> Set rollback: Roll back when a deployment fails | |
--------------------------------------------- | |
Access CodePipeline | |
> Click on pipelines | |
> Create a pipeline | |
> Set a name: "my-pipeline-example" | |
> Select New service role option | |
> Role name: my-pipeline-example-role | |
> Select S3 as Source provider | |
> S3 object key is the file name | |
> Select Amazon CloudWatch Events | |
> Builder provider skip | |
> Select AWS CodeDeploy as Deploy provider | |
> Select application name | |
> Select deploy group | |
--------------------------------------------- | |
Create appspec.yml on your application root | |
You may lint it on: http://www.yamllint.com/ | |
> Example: | |
version: 0.0 | |
os: linux | |
files: | |
- source: ./ | |
destination: /opt/apps | |
hooks: | |
ApplicationStart: | |
- location: scripts/start.sh | |
runas: root | |
ApplicationStop: | |
- location: scripts/stop.sh | |
runas: root | |
Access bitbucket | |
> Create a bitbucket-pipelines.yml file | |
image: atlassian/default-image:2 | |
pipelines: | |
default: | |
- step: | |
name: Build | |
script: | |
- cd app && zip -r ../myapp.zip * | |
artifacts: | |
- myapp.zip | |
- step: | |
name: Upload to S3 | |
services: | |
- docker | |
script: | |
# Test upload | |
- pipe: atlassian/aws-code-deploy:0.2.10 | |
variables: | |
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID} | |
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY} | |
AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION} | |
COMMAND: 'upload' | |
APPLICATION_NAME: ${APPLICATION_NAME} | |
ZIP_FILE: 'myapp.zip' | |
- step: | |
name: Deploy with CodeDeploy | |
deployment: production | |
services: | |
- docker | |
script: | |
# Test upload | |
- pipe: atlassian/aws-code-deploy:0.2.10 | |
variables: | |
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID} | |
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY} | |
AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION} | |
COMMAND: 'deploy' | |
APPLICATION_NAME: ${APPLICATION_NAME} | |
DEPLOYMENT_GROUP: ${DEPLOYMENT_GROUP} | |
IGNORE_APPLICATION_STOP_FAILURES: 'true' | |
FILE_EXISTS_BEHAVIOR: 'OVERWRITE' | |
WAIT: 'true' | |
Refences: | |
https://www.youtube.com/watch?v=OnI_nXa5yH4 | |
https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html | |
https://support.atlassian.com/bitbucket-cloud/docs/deploy-to-aws-with-codedeploy/ | |
https://bitbucket.org/bitbucketpipelines/example-aws-code-deploy/src/master/bitbucket-pipelines.yml | |
http://www.yamllint.com/ | |
https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-example.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment