Know How Guide and Hands on Guide for AWS
Reuse the CodePiple with CodeCommit and CodeDeploy pipeline and add a cross-Region action to a pipeline stage
#!/bin/bash
yum -y update
yum install -y ruby
yum install -y aws-cli
cd /home/ec2-user
aws s3 cp s3://aws-codedeploy-us-east-2/latest/install . --region us-east-2
chmod +x ./install
./install auto
MyDemoApplication
in CodeDeploy
MyDemoDeploymentGroup-Tokyo
MyCodePipelineDemoProduction
CodeDeployDefault.OneAtaTime
Cross-Region
Deploy-Tokyo-Region
, Action provider as AWS CodeDeploy, Region as Tokyo
SourceArtifact
, Application name: MyDemoApplication
, Deployment group: MyDemoDeploymentGroup-Tokyo
. Choose Save.aws codepipeline start-pipeline-execution --name two-stage-pipeline --region ap-southeast-1 --profile global
Follow up the guide to create the (https://docs.aws.amazon.com/codebuild/latest/userguide/getting-started.html)
sample-cross-region
tree
.
├── buildspec.yml
├── pom.xml
└── src
├── main
│ └── java
│ └── MessageUtil.java
└── test
└── java
└── TestMessageUtil.java
5 directories, 4 files
Create the CodePipeline with name cross-region-pipeline
AWS CodeCommit
, Repository name: sample-cross-region
, Branch name: masterSample-Java-MessageUtil
Build project Artifacts setting by add the second artifactcodebuild-region-ID-account-ID-output-bucket
Sample-Java-MessageUtil
Build project can be executed successfullyTHe artifact messageUtil-1.0.jar
has been upload to codebuild-region-ID-account-ID-output-bucket
bucket -> Sample-Java-MessageUtil/target
folder
cross-region-pipeline
pipeline to add the 2 CodeDeploy StagesDeploy
MyAppBuild
which is the output artifact of CodeBuildMyCodePipelineDemo
CodeDeployDefault.OneAtaTime
Deploy
stage with name Production
MyAppBuild
which is the output artifact of CodeBuildCodePipelineProductionFleetGlobal
of MyDemoApplication
MyCodePipelineDemoProduction
CodeDeployDefault.OneAtaTime
ll /tmp/messageUtil-1.0.jar -rw-r–r– 1 root root 2129 Aug 18 01:32 /tmp/messageUtil-1.0.jar
## Create the lambda function to copy the build artifact to China region S3 bucket
1. Create the Secrete `/codepipeline/china/credential` in AWS Secrete Manager in global region
2. Create Lambda execution role with permission of S3 and Secrete Manager
3. Use the `lambda-copy-s3.py` to create your lambda function `lambda-copy-s3` to copy the artifacts from global region s3 bucket to china region s3 bucket
The lambda function environment variables
```bash
S3_BUCKET_NAME_GLOBAL = codebuild-global-region-ID-account-ID-output-bucket
S3_BUCKET_NAME_CHINA = codebuild-china-region-ID-account-ID-output-bucket
Note:
/tmp
storage, if you need large storage than 512MB, you can use the Lambda with AWS EFS#!/bin/bash
yum -y update
yum install -y ruby
yum install -y wget
yum install -y aws-cli
cd /home/ec2-user
wget https://aws-codedeploy-cn-north-1.s3.cn-north-1.amazonaws.com.cn/latest/install
chmod +x ./install
./install auto
service codedeploy-agent status
MyDemoApplication
in CodeDeployCodePipelineProductionFleetChina
under MyDemoApplicationMyCodePipelineDemoProduction
CodeDeployDefault.OneAtaTime
lambda-invoke-codedeploy
lambda-invoke-codedeploy.py
to create your lambda function lambda-invoke-codedeploy
to invoke CodeDeploy in china regionS3_BUCKET_NAME_CHINA = codebuild-china-region-ID-account-ID-output-bucket
S3_ARTIFACT_NAME_CHINA = Sample-Java-MessageUtil.zip
ll /tmp/messageUtil-1.0.jar -rw-r–r– 1 root root 2129 Aug 18 06:18 /tmp/messageUtil-1.0.jar
## Add the Cross region deployment stage to the sample-cross-region CodePipeline
1. Add a cross-Region stage
- Add stage immediately after the Production Deploy stage with name `Cross-Region`
- Add action group with name `Copy_Artifacts_CN`, Action provider as AWS Lambda
- Input artifacts: `MyAppBuild`, Function name: `lambda-copy-s3`
- Add action group with name `Deploy_Artifacts_CN`, Action provider as AWS Lambda
- Input artifacts: `MyAppBuild`, Function name: `lambda-invoke-codedeploy`
![Cross-Region-Pipeline-Stage](/aws-is-how/devops/codepipeline/quickstart/media/Cross-Region-Pipeline-Stage.png)
2. Trigger the new pipeline
```bash
aws codepipeline start-pipeline-execution --name cross-region-pipeline --region ap-southeast-1 --profile global
Here use the Jenkins to create the whole CI/CD pipeline