2 of 3: CI/CD the Easy Way Using AWS CodePipeline

This article is one of three (3) in a series:

1 of 3: CI/CD the Easy Way Using AWS CodePipeline
In part 1, you will use AWS CodePipeline to automate deployment of application changes.

2 of 3: CI/CD the Easy Way Using AWS CodePipeline
In part 2, you will use AWS CodePipeline to add a Testing stage to your pipeline.

3 of 3: CI/CD the Easy Way Using AWS CodePipeline
In part 3, you will use AWS CodePipeline to add Staging and Manual Approval stages to your pipeline. You will decommission the infrastructure once done, because this is only proof-of-concept.

For background on this series, go here:

CI/CD the Easy Way Using AWS CodePipeline | A Three-Part Series

1 of 10. [Cloud9] Create app.py and test_app.py.

Create the 'human-gov-application/src/tests' folder. Inside the test folder, create the files 'app.py' and 'test_app.py'. Add 'pytest' to the 'requirements.txt' at 'human-gov-application/src'

Note: There should be a trailing single quote in the test_app.py. Note that my screenshot does not include one. Additionally, note that the first time I ran the tests, they failed. :D

app.py

def home_page(state): return 'Human Resources Management System - State of ' + state

test_app.py

import pytest from app import home_page def test_home_page(): assert home_page('California') == 'Human Resources Management System - State of California'

requirements.txt

Add the line: pytest

2 of 10. [CodePipeline] Add Test Stage

human-gov-cicd-pipeline -/- [Edit] AFter the 'Build' and before the 'Deploy to Production' click [Add stage] Stage name: HumanGovTest [Add Action Group] Action naem: HumanGovTest Action provider: AWS CodeBuild Region: US East (N. Virgina) Input artifacts: BuildArtifact Project name: Create project Projecct name: HumanGovTest Environment image: Management image Compute: EC2 Operating System: Amazon Linux Runtime(s): Standard Image: aws/codebuild/amazonlinux2-x86_64-standard:4.0 Service role: New service role Role name: Autogenerated Additional configurations: Privileged: Eanble this flag is you want to build Docker images ... VPC: cluster VPC Subnets: the private subnets Security group: eks-cluster-sg-humangov... Buildspec: Insert build commands [Switch to editor] Add the contents of the 'buildspec.yml' below. CloudWatch logs - optional [enabled] [Continue to CodePipeline] Project name: HumanGovTest [Done] [Save]

buildspec.yml

As always, update the authentication to match your push command to the repository.

version: 0.2 phases: build: commands: - # Get Image URI - cd src - IMAGE_URI=$(jq -r '.[0].imageUri' imagedefinitions.json) - echo $IMAGE_URI # Log in to Amazon ECR. - echo Logging in to Amazon ECR... - aws --version - aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/i7y0m4q9 # Pull the Docker image from ECR. - docker pull $IMAGE_URI # Run pytest in the Docker container. - docker run --rm $IMAGE_URI pytest tests/

3 of 10. [IAM] Setup permission to Elastic Container Registry (ECR)

IAM -/- Access management -/- [Roles] [codebuild-HumanGovTest-service-role] Permissions -/- Add permissions -/- [Attach policies] select 'AmazonElasticContainerRegistryPublicFullAccess' [Add permissions]

4 of 10. [Cloud9] commit and push.

cd ~/environment/human-gov-application/src git status git add -A git commit -m "added tests" git push

5 of 10. [CodePipeline] Validate that the test passes in the pipeline.

6 of 10. [Cloud9] Modify test script

In this case, the 'H' in Human is removed, the script should fail now.

test_app.py

import pytest from app import home_page def test_home_page(): assert home_page('California') == 'uman Resources Management System - State of California'

7 of 10. [Cloud9] Commit and push

cd ~/environment/human-gov-application/src git status git add -A git commit -m "Simulating developer mistake - removed H from Human" git push

8 of 10. [CodePipeline] Check the pipeline

The test should fail. Check the Pipeline and logged output.

9 of 10. [Cloud9] Repair the typo, and commit and push

test_app.py

import pytest from app import home_page def test_home_page(): assert home_page('California') == 'Human Resources Management System - State of California' cd ~/environment/human-gov-application/src git status git add -A git commit -m "Fixed developer mistake - added H to Human" git push

10 of 10. [CodePipeline] Confirm the pipeline was repaired.

References

CI/CD Pipeline - AWS CodePipeline - AWS

IAM roles - AWS Identity and Access Management

get-login-password — AWS CLI 1.32.53 Command Reference

What is the AWS Command Line Interface? - AWS Command Line Interface

docker pull | Docker Docs

docker run | Docker Docs

Git - git-add Documentation

Git - git-commit Documentation

Git - git-push Documentation

Git - git-status Documentation

pytest: helps you write better programs — pytest documentation

Title not found

Automatically build and deploy a Java application to Amazon EKS using a CI/CD pipeline - AWS Prescriptive Guidance

Implementing continuous integration and continuous delivery - Practicing Continuous Integration and Continuous Delivery on AWS

Enabling IAM principal access to your cluster - Amazon EKS


Lewis Lampkin, III - Blog

Lewis Lampkin, III - LinkedIn

Lewis Lampkin, III - Medium

Comments

Popular posts from this blog

Containing the Chaos! | A Three-Part Series Demonstrating the Usefulness of Containerization to HumanGov

Orphaned No More: Adopting AWS Lambda