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.
--parameter-overrides: It allows passing an environment variable to the lambda function. We will pass the "MYPARAM" value that we put in the Secret to the Lambda function. When passing an environment variable to a Lambda function, the most crucial part is defining the variables in the "template.yaml" file as well. In the following screenshot, you can see we defined "MyParam" in the "Parameters" section.
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!
Need help in developing serverless applications? Book an Appointment now to achieve benefits of modern application architectures!
Aslı, modern uygulama geliştirmek için sonraki nesil teknolojileri öğrenmeye ve kullanmaya hevesli, turkulu bir Developer'dır. Öğrenme ve yeteneklerini paylaşma arzusu, onu olağanüstü uygulamalar için dönüştürücü pratikler benimsemeye teşvik etmiştir. Sınırları zorlama konusundaki coşkusunu kucaklayarak, olağanüstü çözümler yaratma konusunda tutkuludur.
We use cookies to offer you a better experience.
Kişiselleştirilmiş içerikle size daha iyi bir deneyim sunmak için çerezleri kullanıyoruz.
Çerezler, ziyaret ettiğiniz web siteleri tarafından bilgisayarınıza gönderilen ve saklanan küçük dosyalardır. Bir sonraki ziyaretinizde tarayıcınız çerezi okuyarak bilgileri, çerezi oluşturan web sitesine veya öğeye iletir.
ㅤㅤㅤㅤㅤㅤ
Çerezler, web sitemizi her ziyaret ettiğinizde sizi otomatik olarak tanımamıza yardımcı olur, böylece deneyiminizi kişiselleştirebilir ve size daha iyi hizmet sunabiliriz.