{{announcement.body}}
{{announcement.title}}

Automate Application Deployment Using Github Actions

DZone 's Guide to

Automate Application Deployment Using Github Actions

Learn how to automate deployment of serverless on AWS using GitHub Actions.

· Cloud Zone ·
Free Resource

GitHub Actions makes it easy to automate all your software workflows. You can build, test, and deploy your code right from GitHub. In this post we will explore how you can use GitHub Actions to automate serverless application deployment on AWS. You can also use AWS's own CI/CD services to achieve the same. But here we are going to keep our discussion limited to GitHub Actions. 

How to Use GitHub Actions?

Creating a GitHub action is simple. Go to your GitHub repository that you want to automate and click on "Actions"


You will be taken to Actions page where you can create a new Blank workflow or select existing actions from the marketplace. The actions from marketplace are reusable actions that you can use in your workflow. We are going to create a blank action and we will also use some actions from marketplace. 

Let's rename the YAML file to workflow.yml. You can name anything you like. We are going to create a Lambda function with API gateway in Serverless Application Model (SAM) template and deploy it using GitHub Actions. Below is our SAM template. 

YAML
 




x
32


 
1
AWSTemplateFormatVersion: '2010-09-09'
2
Transform: AWS::Serverless-2016-10-31
3
Description: >
4
  GitHub Actions demonstration App
5
Resources:
6
  ApiGatewayApi:
7
    Type: AWS::Serverless::Api
8
    Properties:
9
      StageName: Prod
10
    Auth:
11
     UsagePlan:
12
      CreateUsagePlan: PER_API
13
      Description: Usage plan for this API
14
      Quota:
15
       Limit: 500
16
       Period: MONTH
17
      Throttle:
18
       BurstLimit: 100
19
       RateLimit: 50
20
  LamdbaFunction:
21
    Type: AWS::Serverless::Function
22
    Properties:
23
      CodeUri: ./    
24
      Handler: lambda.handler
25
      Runtime: python3.8
26
      Events:
27
        getCounter:
28
          Type: Api
29
          Properties:
30
            Path: /hello
31
            Method: get
32
            RestApiId: !Ref ApiGatewayApi


lambda.py 

Python
 




xxxxxxxxxx
1
11


1
def handler(event, context):
2
            return {
3
        'statusCode': 200,
4
        'headers': {
5
            'Content-Type': 'application/json',
6
            'Access-Control-Allow-Origin': '*'
7
        },
8
        'body':'Hello from Lambda!'
9
        ,
10
        "isBase64Encoded": False
11
    }


This contains one Lambda function and API with path hello. Lets first deploy manually using SAM CLI and then we will automate it. Create samconfig.toml with below details. create s3_bucket that will be used for SAM deploy and update in samconfig.toml. 

Properties files
 




xxxxxxxxxx
1
10


1
version = 0.1
2
[default]
3
[default.deploy]
4
[default.deploy.parameters]
5
stack_name = "sam-github-actions-app"
6
s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-1xyg1t2j2ws5k"
7
s3_prefix = "sam-app"
8
region = "us-east-1"
9
confirm_changeset = false
10
capabilities = "CAPABILITY_IAM"


Also, create empty requirements.txt along with template.yml. Run SAM build and SAM deploy -g on CLI. 

Running SAM build and SAM deploy

Go to API gateway and hit the URL in browser. You should get "hello from Lambda!" response. 

Go to API gateway


Go back to our workflow file on GitHub. We will deploy as soon as we push updates to the repo. Below is our workflow.yml 

YAML
 




xxxxxxxxxx
1
51


1
# This is a basic workflow to help you get started with Actions
2
name: AWS Lambda & API gateway deployment demonstration
3
# Controls when the action will run. Triggers the workflow on push or pull request
4
# events but only for the master branch
5
on:
6
  push:
7
    branches: [ master ]
8
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
9
jobs:
10
  # This workflow contains a single job called "build"
11
  build:
12
    # The type of runner that the job will run on
13
    runs-on: ubuntu-latest
14
    # Steps represent a sequence of tasks that will be executed as part of the job
15
    steps:
16
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
17
    - uses: actions/checkout@v2
18
    # Installs Python
19
    - name: Set up Python 3.8
20
      uses: actions/setup-python@v2
21
      with:
22
        python-version: 3.8
23
    # Installs PIP
24
    - name: Install dependencies
25
      run: |
26
        python -m pip install --upgrade pip
27
    # Configures AWS credentials from github secrets
28
    - name: Configure AWS Credentials
29
      uses: aws-actions/configure-aws-credentials@v1
30
      with:
31
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
32
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
33
        aws-region: us-east-1
34
    # Build using SAM 
35
    - name: SAM Build
36
      uses: youyo/aws-sam-action/python3.8@master
37
      with:
38
        sam_command: build
39
      env:
40
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
41
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
42
          AWS_DEFAULT_REGION:  us-east-1
43
    # Deploy on AWS
44
    - name: sam deploy
45
      uses: youyo/aws-sam-action/python3.8@master
46
      with:
47
          sam_command: 'deploy --stack-name myApp --no-fail-on-empty-changeset'
48
      env:
49
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
50
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
51
          AWS_DEFAULT_REGION: us-east-1


We first indicate that we want to run this action on push to the master. Then we select runner (Ubuntu) on which our steps will execute. In Steps, we first checkout code, then install python and its dependencies. Then we use other actions from marketplace to configure AWS credentials and then we use another action to do SAM build and SAM deploy. Please note that we need to supply AWS ACCESS KEY and SECRET ACCESS KEY to actions for commands to work. We set up here GitHub Secrets. 

Github Secrets

As soon as you check-in workflow.yml, the action would trigger. 

Starting workflow run

Deploying stack 

Deploying stack


Completed job

Now you can login to the AWS console and confirm the stack is created. 

Confirm stack creation

Go to Resources tab and access the API Gateway. Go to stages and access the Prod stage API. Open the URL in browser with the path /hello and you should see below output! 

Openning URL

Congratulations! You have successfully automated AWS deployment using GitHub Actions! You can download the code from here: 

https://github.com/rajanpanchal/aws-github-actions


Topics:
automation, aws, deployment automation, github actions, how-to guides

Published at DZone with permission of Rajan Panchal . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}