0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「AWS Lambda now supports GitHub Actions to simplify function deployment」を試してみた。

Posted at

はじめに

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の結果を応答します。`

lambda_function.py
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}'
    }

以下のワークフローのサンプルコードを少しだけいじってデプロイしてみます。

.github/workflows/deploy.yaml
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の指定が必要みたい。

スクリーンショット 2025-08-17 6.09.31.png

新規作成用にロール等を指定してみる

以下ページを参考にroleを指定してみます。

.github/workflows/deploy.yaml
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になってる!

image.png

他にもruntimehandlerを指定しないといけないみたい。

.github/workflows/deploy.yaml
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: ./

これで無事デプロイできました。

image.png

外部ライブラリの利用

あとはrequestsなどの外部ライブラリをインストールしてみます。

今回は試してませんが、レイヤーの指定とかもできるみたいです。

.github/workflows/deploy.yaml
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も使えるようになりました!

image.png

まとめ

ソースは以下に格納しておきました。

AWS SAMを使えばいいかなとも思いますが、Lambda単体で運用したい場合とかは便利なのかな?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?