How To Build a Serverless CI/CD Pipeline On AWS

Gavin Lewis
Jan 15, 2019 · 9 min read
Image for post
Image for post

Getting Prepared


1. Create an EC2 instance and install Docker

sudo yum update -y
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user
docker info
Image for post
Image for post
Output of the docker info command

2. Build Docker image and push to ECR

git clone https://github.com/aws/aws-codebuild-docker-images.git
cd aws-codebuild-docker-images
cd ubuntu/dot-net/core-2.1/
nano Dockerfile
# Install Node Dependencies
ENV NODE_VERSION="10.14.1"
# gpg keys listed at https://github.com/nodejs/node#release-team
RUN set -ex \
&& for key in \
94AE36675C464D64BAFA68DD7434390BDBE9B9C5 \
B9AE9905FFD7803F25714661B63B535A4C206CA9 \
77984A986EBC2AA786BC0F66B01FBB92821C587A \
56730D5401028683275BD23C23EFEFE93C4CFFFE \
71DCFD284A79C3B38668286BC97EC7A07EDE3FC1 \
FD3A5288F042B6850C66B31F09FE44734EB7990E \
8FCCA13FEF1D0C2E91008E09770F7A9A5AE15600 \
C4F0DFFF4E8C1A8236409D08E73BC641CC11F4C8 \
DD8F2338BAE7501E3DD5AC78C273792F7D83545D \
4ED778F539E3634C779C87C6D7062848A1AB005C \
A48C2BEE680E841632CD4E44F07496B3EB3C1762 \
; do \
gpg - keyserver hkp://p80.pool.sks-keyservers.net:80 - recv-keys "$key" || \
gpg - keyserver hkp://ipv4.pool.sks-keyservers.net - recv-keys "$key" || \
gpg - keyserver hkp://pgp.mit.edu:80 - recv-keys "$key" ; \
done
RUN set -ex \
&& wget "https://nodejs.org/download/release/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz" -O node-v$NODE_VER$
&& wget "https://nodejs.org/download/release/v$NODE_VERSION/SHASUMS256.txt.asc" -O SHASUMS256.txt.asc \
&& gpg - batch - decrypt - output SHASUMS256.txt SHASUMS256.txt.asc \
&& grep " node-v$NODE_VERSION-linux-x64.tar.gz\$" SHASUMS256.txt | sha256sum -c - \
&& tar -xzf "node-v$NODE_VERSION-linux-x64.tar.gz" -C /usr/local - strip-components=1 \
&& rm "node-v$NODE_VERSION-linux-x64.tar.gz" SHASUMS256.txt.asc SHASUMS256.txt \
&& ln -s /usr/local/bin/node /usr/local/bin/nodejs \
&& rm -fr /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN npm set unsafe-perm trueCMD [ "node" ]# Install Serverless Framework
RUN set -ex \
&& npm install -g serverless
docker build -t aws/codebuild/dot-net .
docker run -it --entrypoint sh aws/codebuild/dot-net -c bashsls -v
Image for post
Image for post
Running sls -v inside of the newly created container
aws ecr create-repository --repository-name codebuild-dotnet-node
Image for post
Image for post
Response from the AWS CLI after creating the repository in ECR
Image for post
Image for post
The newly created repository in ECR
docker tag aws/codebuild/dot-net <ACCOUNTID>.dkr.ecr.ap-southeast-2.amazonaws.com/codebuild-dotnet-node
aws ecr get-login --no-include-email
Image for post
Image for post
The login command to authenticate into ECR
docker login -u AWS -p eyJwYXlsb2FkIjoiNGZnd0dSaXM1L2svWWRLMmhJT1c0WWpOZEcxamJFeFJOK2VvT0Y5[...] https://<ACCOUNTID>.dkr.ecr.ap-southeast-2.amazonaws.com
docker push <ACCOUNTID>.dkr.ecr.ap-southeast-2.amazonaws.com/codebuild-dotnet-node
Image for post
Image for post
Docker on EC2 is creating our image
Image for post
Image for post
The Docker image in ECR
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "EnableAccountAccess",
"Effect": "Allow",
"Principal": "*",
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:DescribeImages",
"ecr:DescribeRepositories",
"ecr:GetAuthorizationToken",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:ListImages"
]
}
]
}

3. Create your Pipeline

DockerImageArn: <ACCOUNTID>.dkr.ecr.ap-southeast-2.amazonaws.com/codebuild-dotnet-node:latest
DeploymentFilename: Deployment.zip
sls deploy -v
Image for post
Image for post
Our CloudFormation stack created by the serverless framework
Image for post
Image for post
The CodePipeline Pipeline created by the serverless framework
Image for post
Image for post
The CodeBuild Project created by the serverless framework

4. Add buildSpec.yml to your application

version: 0.2phases:
pre_build:
commands:
- chmod a+x *
build:
commands:
- ./build.sh
post_build:
commands:
- sls deploy -v -s $STAGE

5. Testing your Pipeline

Image for post
Image for post
Deployment.zip has been uploaded to S3
Image for post
Image for post
CodePipeline has begun and the build is in progress
Image for post
Image for post
CodeBuild will get the output from the Docker image performing the build and deployment
Image for post
Image for post
Our deployment was successful!
Image for post
Image for post
Image for post
Image for post
Using Postman to invoke our API

Summary

Image for post
Image for post
Image for post
Image for post

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

FAUN

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Gavin Lewis

Written by

Passionate about building and delivering solutions in the Cloud! Senior Cloud Architect @ Rapid Circle. Views are my own.

FAUN

FAUN

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

Gavin Lewis

Written by

Passionate about building and delivering solutions in the Cloud! Senior Cloud Architect @ Rapid Circle. Views are my own.

FAUN

FAUN

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.