Deploying to Amazon Web Services Lambda from GitHub Actions
Introduction
GitHub Actions is a powerful tool for automating workflows on GitHub. One of its most useful features is the ability to deploy applications to various cloud platforms, including Amazon Web Services (AWS). In this article, we will show you how to deploy to Amazon Web Services Lambda from GitHub Actions.
Prerequisites
Before we dive into the steps, make sure you have the following:
- A GitHub repository with your application code
- A GitHub Actions workflow file (
.ymlfile) that defines the deployment process - An AWS account with the necessary permissions to create and manage Lambda functions
- An AWS Lambda function that you want to deploy from GitHub Actions
Step 1: Create a GitHub Actions workflow file
Create a new file in your repository’s .github/workflows directory, e.g., lambda-deploy.yml. This file will define the deployment process.
name: Lambda Deploy
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Login to AWS
uses: aws-actions/login@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Create Lambda function
uses: aws-actions/create-lambda@v1
with:
function-name: ${AWS Lambda function name}
handler: index.handler
runtime: nodejs14.x
code: |
const AWS = require('aws-sdk');
AWS.config.update({
region: 'your-region',
accessKeyId: 'your-access-key-id',
secretAccessKey: 'your-secret-access-key'
});
module.exports = {
handler: 'index.handler',
event: 'aws:s3:ObjectCreated:ObjectCreatedEvent'
};
name: ${AWS Lambda function name}
- name: Deploy to Lambda
uses: aws-actions/ deploy-lambda@v1
with:
function-name: ${AWS Lambda function name}
handler: index.handler
runtime: nodejs14.x
code: |
const AWS = require('aws-sdk');
AWS.config.update({
region: 'your-region',
accessKeyId: 'your-access-key-id',
secretAccessKey: 'your-secret-access-key'
});
module.exports = {
handler: 'index.handler',
event: 'aws:s3:ObjectCreated:ObjectCreatedEvent'
};
name: ${AWS Lambda function name}
Step 2: Define the AWS credentials
In the lambda-deploy.yml file, you need to define the AWS credentials. You can do this by adding the following code:
aws:
accessKeyId: ${{ secrets.AWS_ACCESS_KEY_ID }}
secretAccessKey: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
region: ${{ secrets.AWS_REGION }}
Step 3: Store AWS credentials as secrets
To store the AWS credentials as secrets, you can use the following code:
name: Store AWS credentials as secrets
on:
push:
branches:
- main
jobs:
store-credentials:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Store AWS credentials as secrets
uses: actions/ secrets@v1
with:
name: AWS_ACCESS_KEY_ID
name: AWS_SECRET_ACCESS_KEY
name: AWS_REGION
Step 4: Create an AWS Lambda function
Create a new AWS Lambda function that you want to deploy from GitHub Actions. You can do this by following these steps:
- Go to the AWS Management Console and navigate to the Lambda dashboard.
- Click on "Create function" and choose "Author from scratch".
- Choose "Node.js" as the runtime and "AWS Lambda" as the runtime.
- Choose "Node.js 14.x" as the version.
- Choose "Event" as the event type and "aws:s3:ObjectCreated:ObjectCreatedEvent" as the event type.
- Choose "Function name" and enter a name for your function.
- Choose "Handler" and enter the
index.handlerfile. - Choose "Code" and enter the following code:
exports.handler = async (event) => {
const s3 = new AWS.S3({
region: 'your-region',
accessKeyId: 'your-access-key-id',
secretAccessKey: 'your-secret-access-key'
});
const params = {
Bucket: 'your-bucket-name',
Key: event.Records[0].s3.object.key
};
const data = await s3.getObject(params).promise();
return {
statusCode: 200,
body: JSON.stringify(data.Body)
};
};
Step 5: Deploy to AWS Lambda
Finally, you can deploy to AWS Lambda using the following code:
name: Deploy to AWS Lambda
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Login to AWS
uses: aws-actions/login@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Create Lambda function
uses: aws-actions/create-lambda@v1
with:
function-name: ${AWS Lambda function name}
handler: index.handler
runtime: nodejs14.x
code: |
const AWS = require('aws-sdk');
AWS.config.update({
region: 'your-region',
accessKeyId: 'your-access-key-id',
secretAccessKey: 'your-secret-access-key'
});
module.exports = {
handler: 'index.handler',
event: 'aws:s3:ObjectCreated:ObjectCreatedEvent'
};
name: ${AWS Lambda function name}
- name: Deploy to AWS Lambda
uses: aws-actions/ deploy-lambda@v1
with:
function-name: ${AWS Lambda function name}
handler: index.handler
runtime: nodejs14.x
code: |
const AWS = require('aws-sdk');
AWS.config.update({
region: 'your-region',
accessKeyId: 'your-access-key-id',
secretAccessKey: 'your-secret-access-key'
});
module.exports = {
handler: 'index.handler',
event: 'aws:s3:ObjectCreated:ObjectCreatedEvent'
};
name: ${AWS Lambda function name}
Conclusion
In this article, we have shown you how to deploy to Amazon Web Services Lambda from GitHub Actions. We have covered the steps involved in creating a GitHub Actions workflow file, defining AWS credentials, creating an AWS Lambda function, and deploying to AWS Lambda. By following these steps, you can automate the deployment of your applications to AWS Lambda from your GitHub repository.
