はじめに
2025/8/7にこんなアップデートがありました。
AWS Lambda now enables you to use GitHub Actions to automatically deploy Lambda functions when you push code or configuration changes to your GitHub repository, streamlining your continuous integration and continuous deployment (CI/CD) pipeline for serverless applications.
少し前にこんな記事を書いたのですが、これより少し楽にLambdaのCI/CD環境ができるようになったよ。って感じかな。
やってみた。
とりあえず動かしてみる。
今回動作確認するPythonコードは以下。
GitHub禅APIhttps://api.github.com/zenの結果を応答します。`
import requests
def handler(event, context):
response = requests.get('https://api.github.com/zen')
return {
'statusCode': 200,
'body': f'Hello Github actions for lambda! GitHub says: {response.text}'
}
以下のワークフローのサンプルコードを少しだけいじってデプロイしてみます。
name: Deploy AWS Lambda
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
environment: develop
permissions:
id-token: write # Required for OIDC authentication
contents: read # Required to check out the repository
steps:
- uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ vars.AWS_ROLE_ARN }}
aws-region: ap-northeast-1
- name: Deploy Lambda Function
uses: aws-actions/aws-lambda-deploy@v1
with:
function-name: my-lambda-function
code-artifacts-dir: ./
エラー出ました。
新たに作るときはロールArnの指定が必要みたい。
新規作成用にロール等を指定してみる
以下ページを参考にroleを指定してみます。
name: Deploy AWS Lambda
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
environment: develop
permissions:
id-token: write # Required for OIDC authentication
contents: read # Required to check out the repository
steps:
- uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ vars.AWS_ROLE_ARN }}
aws-region: ap-northeast-1
- name: Deploy Lambda Function
uses: aws-actions/aws-lambda-deploy@v1
with:
function-name: my-lambda-function
+ role: ${{vars.LAMBDA_ROLE_ARN}}
code-artifacts-dir: ./
できた!...けど、Node.js 20.xになってる!
他にもruntimeやhandlerを指定しないといけないみたい。
name: Deploy AWS Lambda
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
environment: develop
permissions:
id-token: write # Required for OIDC authentication
contents: read # Required to check out the repository
steps:
- uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ vars.AWS_ROLE_ARN }}
aws-region: ap-northeast-1
- name: Deploy Lambda Function
uses: aws-actions/aws-lambda-deploy@v1
with:
function-name: my-lambda-function
+ runtime: python3.12
+ handler: lambda_function.handler
role: ${{vars.LAMBDA_ROLE_ARN}}
code-artifacts-dir: ./
これで無事デプロイできました。
外部ライブラリの利用
あとはrequestsなどの外部ライブラリをインストールしてみます。
今回は試してませんが、レイヤーの指定とかもできるみたいです。
name: Deploy AWS Lambda
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
environment: develop
permissions:
id-token: write # Required for OIDC authentication
contents: read # Required to check out the repository
steps:
- uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ vars.AWS_ROLE_ARN }}
aws-region: ap-northeast-1
+ - name: Build source code using setup tools
+ run: |
+ # Install dependencies
+ pip install -r requirements.txt -t ./dist
+ # Move source file
+ mv lambda_function.py ./dist
- name: Deploy Lambda Function
uses: aws-actions/aws-lambda-deploy@v1
with:
function-name: my-lambda-function
runtime: python3.12
handler: lambda_function.handler
role: ${{vars.LAMBDA_ROLE_ARN}}
+ code-artifacts-dir: ./dist
これでrequestsも使えるようになりました!
まとめ
ソースは以下に格納しておきました。
AWS SAMを使えばいいかなとも思いますが、Lambda単体で運用したい場合とかは便利なのかな?



