Giter Club home page Giter Club logo

github-action-aws-cloudformation's Introduction

AWS CloudFormation GitHub Action

Build, Test & Deploy CodeQL

A GitHub Action to create/update your CloudFormation stack to support Infrastructure as Code.

Motivation

The official CloudFormation action is archived with no explanation why.

This Action is actively maintained and includes additional features.

Features

  • Apply or Preview ChangeSet with Pull Request comments
  • Log intervals to show constant feedback
  • CloudFormation outputs set as Action Outputs (which can be used in subsequent steps)
  • Template validation

Getting Started

Please read https://github.com/aws-actions/configure-aws-credentials#credentials

name: 'deploy'

concurrency:
  group: prod_deploy
  cancel-in-progress: false

on:
  repository_dispatch:
  pull_request:
  push:
    branches:
      - main
      - 'releases/*'

jobs:
  deploy-stack:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1

      - name: Update CloudFormation Stack
        id: update-stack
        uses: badsyntax/github-action-aws-cloudformation@master
        with:
          stack-name: 'example-cloudformation-stack'
          template: './cloudformation/s3bucket-example.yml'
          # Only apply the changeset on pushes to main/release
          apply-change-set: ${{ github.event_name != 'pull_request' && github.event_name != 'repository_dispatch' }}
          aws-region: 'us-east-1'
          parameters: 'S3BucketName=example-bucket-us-east-1&S3AllowedOrigins=https://example.com'

      - name: Deploy Website
        run: |
          # Now that the stack is created we can deploy our
          # website to the S3 bucket.
          echo "Deploy to S3 Bucket: $S3BucketName"
        env:
          # Use outputs from the CloudFormation Stack
          S3BucketName: ${{ steps.update-stack.outputs.S3BucketName }}

Action Inputs

key description example
stack-name The name of the Cloudformation stack to be created example-com-static-cloudformation-stack
template The relative path to the CloudFormation stack template ./cloudformation/s3bucket_with_cloudfront.yml
aws-region The AWS region in which to create the stack us-east-1
parameters (optional) The parameters to override in the stack inputs, in query string format. Whitespace will be stripped Param1=foo&Param2=http://example.com
apply-change-set Whether to apply the ChangeSet, or provide a summary of the ChangeSet true
capabilities (optional) A comma-delimited list of stack template capabilities to acknowledge. Defaults to CAPABILITY_IAM CAPABILITY_IAM

Action Outputs

Name Description Example
[cf-output-key] Outputs correspond to the CloudFormation outputs. For example if you've set an output to be S3BucketName then this key will exist as an Action output anything, depending on the CF output definition
outputs JSON string array of CloudFormation outputs [{"OutputKey":"S3BucketName","OutputValue":"bucket-name","Description":"Bucket name"}]
changes JSON string array of CloudFormation changes [{"OutputKey":"S3BucketName","OutputValue":"bucket-name","Description":"Bucket name"}]

Pull Request Comments

First you need to define the comment template. Download pr-comment-template.hbs to your repo and update as appropriate.

Next add the badsyntax/github-action-issue-comment action to create Pull Request comments with an overview of the CloudFormation changes and outputs.

name: 'deploy'

concurrency:
  group: prod_deploy
  cancel-in-progress: false

on:
  repository_dispatch:
  pull_request:
  push:
    branches:
      - main
      - 'releases/*'

jobs:
  deploy-stack:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1

      - name: Update CloudFormation Stack
        id: update-stack
        uses: badsyntax/github-action-aws-cloudformation@master
        with:
          stack-name: 'example-cloudformation-stack'
          template: './cloudformation/s3bucket-example.yml'
          # Only apply the changeset on pushes to main/release
          apply-change-set: ${{ github.event_name != 'pull_request' && github.event_name != 'repository_dispatch' }}
          aws-region: 'us-east-1'
          parameters: 'S3BucketName=example-bucket-us-east-1&S3AllowedOrigins=https://example.com'

      - uses: badsyntax/github-action-issue-comment@master
        name: Comment on Pull Request
        if: github.event_name == 'pull_request'
        with:
          action: 'create-clean'
          template: '.github/pr-comment-template.hbs'
          id: cloudformation
          token: ${{ secrets.GITHUB_TOKEN }}
          issue-number: ${{ github.event.pull_request.number }}
          template-inputs: |
            {
              "changes": ${{ steps.update-stack.outputs.changes }},
              "outputs": ${{ steps.update-stack.outputs.outputs }},
              "applyChangeSet": ${{ github.event_name != 'pull_request' && github.event_name != 'repository_dispatch' }}
            }

      - name: Deploy Website
        run: |
          # Now that the stack is created we can deploy our
          # website to the S3 bucket.
          echo "Deploy to S3 Bucket: $S3BucketName"
        env:
          # Use outputs from the CloudFormation Stack
          S3BucketName: ${{ steps.update-stack.outputs.S3BucketName }}

ScreenShots

Pull request created and apply-change-set is false:

Pull Request Comment

Pull request created and apply-change-set is true:

Pull Request Comment

No stack changes:

Pull Request Comment

Related Projects

Debugging

Check the Action output for logs.

If you need to see more verbose logs you can set ACTIONS_STEP_DEBUG to true as an Action Secret.

Support

License

See LICENSE.md.

github-action-aws-cloudformation's People

Contributors

badsyntax avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.