Github Actions allows you to deploy Lambda functions directly from GitHub. It is possible to create workflows for actions such as managing, sharing, and distributing code with GitHub Actions. In other words, we can make the codes interact with various concepts. Many different actions such as compiling, packaging, and distributing the codes, running them on various platforms, creating status updates and notifications, and issue management can be performed with GitHub Actions.
In this article, we will go through how to deploy a Lambda function with GitHub Actions. Github Actions allows us to automate our deployment process without managing additional infrastructure.
Let’s start with creating an empty repository on Github and call it lambda-action-test
, and after creating the repository, develop a lambda function that outputs Hello World
on AWS.
SAM (Serverless Application Model) framework offered by AWS, which allows you to build serverless applications, will be used to create the Lambda function.
You can follow the necessary steps to download SAM from the following link;
https://aws.amazon.com/serverless/sam
We will create a .github/workflows
directory in the project folder and create the main.yml
file in it.
name: Python application
on:
push:
branches:
- main
jobs:
deploy-lambda:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: aws-actions/setup-sam@v1
- 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-2
# Build using SAM
- run: sam build --use-container --template-file sam-app/template.yaml
# Deploy on AWS
- run: sam deploy --config-file sam-app/samconfig.toml --no-fail-on-empty-changeset --parameter-overrides ParameterKey=MyParam,ParameterValue=${{ secrets.MyParam }}
on parameter: The part where we specify in which state the action will be triggered. In this case, the code will start to deploy as soon as you merge it into the main branch.
jobs: The part where we specify what the Action should do. A job runs in a runner environment. In our case, we use Ubuntu. This is specified on runs-on.
steps: The part we define the steps to take during the deployment process. With uses we define what the step will do. With statement allows us to pass parameters to uses statements.
Recently AWS launched aws-actions/setup-sam@v1
that lets us deploy our lambda functions from GitHub actions. With aws-actions/setup-sam@v1
command, we can run AWS SAM CLI commands inside a workflow.
We can define our environment variables or store sensitive information on Github secrets. To add a secret go to the Secret section under Settings in the repository. In the next step, we pass the secret we define here.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Parameters:
MyParam:
Description: "MyParam"
Type: "String"
Description: >
sam-app
Sample SAM Template for sam-app
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
FunctionName: hello-world-github
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get
Environment:
Variables:
MyParam: !Ref MyParam
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
Lastly, print the value we passed as env variable in our app.py
file.
return {
"statusCode": 200,
"body": json.dumps({
"abc": ip.status_code,
"message": os.environ.get('MyParam')
}),
}
We can track the deployment steps and status under the Actions tab after pushing the code to main branch.
After the job is completed, go to the Lambda function we created from the AWS console and check if the environment variable passed.
We can see the output of the function by running the command with AWS CLI:
$ aws lambda invoke --function-name hello-world-github /tmp/response.json
$ cat /tmp/response.json
{"statusCode": 200, "body": "{\"MyParam\": \"12345678\", \"message\": \"Hello world\"}"}
You have just deployed your Lambda with GitHub Actions, congratulations! We hope this article will help you to build your own CI/CD pipeline to deploy your serverless applications seamlessly. Thank you for reading, stay tuned for different articles!
Aslı is a dedicated developer who is eager to learn and use next-generation technologies to modern application development. Her desire for learning and sharing skills and emerging technologies has prompted her to adopt transformative practices for outstanding apps. She embraces her enthusiasm for pushing the boundaries to create exceptional solutions.
Subscribe to Our Newsletter
Our Service
Specialties
Copyright © 2018-2024 Sufle
We use cookies to offer you a better experience with personalized content.
Cookies are small files that are sent to and stored in your computer by the websites you visit. Next time you visit the site, your browser will read the cookie and relay the information back to the website or element that originally set the cookie.
Cookies allow us to recognize you automatically whenever you visit our site so that we can personalize your experience and provide you with better service.