soren / github-to-ec2-pipeline

A GitHub-to-EC2 CI/CD template using CodeBuild and CodeDeploy.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A GitHub-to-EC2 CI/CD Pipeline in 7 Clicks

This is a simple template for creating a CI/CD pipeline that automatically picks up new commits from GitHub, builds them with AWS CodeBuild, and deploys them to EC2 using AWS CodeDeploy. I've extracted this from a project I'm working on.

You can try this in your AWS account in about 7 clicks. The final result will be a fully working Node.js demo app that gets built and deployed to an EC2 instance in your account, accessible via HTTPS using a Let's Encrypt certificate.

Setup From the Console

  1. Fork this repo into your GitHub account, or create a new repo using this one as a template.

  2. Create a new GitHub Personal Access Token to let AWS read your GitHub repos and set up a webhook. Give it full "repo" and "admin:repo_hook" permissions.

  1. Click the button below to start setting up all the AWS resources in your account.

Launch Stack

At the end of this setup you will end up with an EC2 instance, an S3 bucket, and a CodePipeline using CodeBuild and CodeDeploy. You will be able to easily tear down all resources by deleting the CloudFormation stack. Only the S3 bucket needs to be deleted manually. You can find the full template here: deploy/infra.yml.

  1. Fill in all the required parameters.

  1. Wait for the CloudFormation stack to finish and find the output URL.

  1. Click on the output URL to see the demo web app.

  1. You can now find your new CI/CD pipeline in the CodePipeline console.

  1. Now when you push a new change to GitHub, the pipeline will pick it up, build it, and deploy it. The demo app takes about 3 minutes to go through the pipeline.

Setup From the AWS CLI

  1. Go through steps 1 and 2 from the console instructions.

  2. Put your GitHub Personal Access Token in ~/.github/access-token:

mkdir -p  ~/.github/access-token
ehco "<YOUR TOKEN" >  ~/.github/access-token
  1. Checkout your repo:
git clone https://github.com/<YOUR GIHUB USERNAME>/github-to-ec2-pipeline.git
  1. Run the CloudFormation script using the AWS CLI, and then follow steps 4 to 7 from the console instructions:
aws cloudformation deploy \
    --region us-west-2 \
    --stack-name MyApp \
    --template-file ./deploy/infra.yml \
    --no-fail-on-empty-changeset \
    --parameter-overrides \
      GitHubOwner=dvassallo \
      GitHubRepo=github-to-ec2-pipeline \
      GitHubBranch=master \
      GitHubPersonalAccessToken=$(cat ~/.github/access-token) \
      EC2InstanceType=t3.micro \
      EC2KeyPair=dvassallo \
      EC2VPC=vpc-279a8c42 \
      EC2Subnet=subnet-533dd837 \
      Domain=vassallo.io \
    --capabilities CAPABILITY_NAMED_IAM

These are all the available options for parameter-overrides:

GitHub Configuration

  • GitHubOwner: The username of the source GitHub repo.
  • GitHubRepo: The source GitHub repo name (without the username).
  • GitHubBranch: The source GitHub branch. Default: master.
  • GitHubPersonalAccessToken: Your GitHub personal access token to let AWS read your repos and set up a webhook.

EC2 Configuration

  • EC2InstanceType: The staging host EC2 instance type. Only tested on x86_64. Default: t3.medium.
  • EC2AMI: The EC2 AMI. Only tested on Amazon Linux 2. Default: The latest Amazon Linux 2 AMI in the region.
  • EC2KeyPair: [Optional] An existing EC2 keypair to be able to ssh to your staging host.
  • EC2VPC: The VPC where to run the staging host.
  • EC2Subnet: The subnet for the staging host.

HTTPS Configuration

  • Domain: [Optional] Your root domain name (Example: example.com). HTTPS will only be enabled if a domain is specified. Only provide this if your DNS is managed by Route 53.

License

This project is released under the MIT License.

About

A GitHub-to-EC2 CI/CD template using CodeBuild and CodeDeploy.

License:MIT License


Languages

Language:JavaScript 88.4%Language:CSS 5.1%Language:Shell 4.1%Language:HTML 2.4%