This is a simple exercise to create a Lambda function using AWS CDK and GitHub Actions. Follow the steps below to create a GitHub Actions workflow that will deploy a Lambda function using the code in this repository.
Create a new file in the .github/workflows directory called deploy.yml and add the following code:
name: Deploy Lambda Function
on:
push:
branches:
- '**'
pull_request:
branches: [main]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Print hello world
run: echo "Hello, world!"
Commit and push the changes to the repository. Check that the workflow runs successfully in the Actions tab of the repository. The workflow_dispatch event allows you to manually trigger the workflow from the Actions tab. Try to run the workflow manually and check that it runs successfully.
Use the action/setup-node action to configure Node.js version 22 in the run environment. Add a step with the "uses:" keyword to reference the action. See action's documentation for usage details.
Add a step that runs npm ci using the "run:" keyword.
Add a step that runs npm install -g aws-cdk using the "run:" keyword.
Add the following step to the job:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@master
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
aws-region: "us-east-1"Go to repository settings => Secrets and variables => Actions => New repository secret and add AWS access and secret keys using the names AWS_ACCESS_KEY and AWS_SECRET_KEY.
- Add a step that runs
cdk deployusing the "run:" keyword - Commit and push, browse to the Actions tab and check that the workflow runs successfully
- Go to the logs and check that the Lambda function was deployed
- Browse to the URL of the Lambda function found in the logs and see that it's working.