Lambda 関数の更新を AWS にデプロイする
In the following guide, you'll learn the process of updating an existing Lambda function in AWS using aws-lambda-deploy pipe, as well as configuring aliases for your Lambda functions and use them to promote the newly published version through, test, staging, and production environments. Check out the repository containing the examples you'll see on this page.
前提条件
以下が必要です。
AWS コンソールへのアクセス。
既存の Lambda 関数。
Lambda 関数を更新するのに十分なアクセス許可とアクセス権を持つ IAM ユーザー
AWS have tutorials for setting up an IAM user and how to create a Lambda function using the AWS console if you don't have these done already.
AWSLambdaFullAccess ポリシーを IAM ユーザーに関連付け、Lambda 関数を操作するための完全な権限を付与することをおすすめします。
AWS 資格情報を Bitbucket Pipelines に追加する
You'll need to add 3 variables to Bitbucket Pipelines containing the credentials of the IAM user that will be used to update the Lambda function:
AWS_ACCESS_KEY_ID: IAM ユーザーの AWS アクセス キー。
AWS_SECRET_ACCESS_KEY: IAM ユーザーの AWS 秘密アクセス キー。セキュアな変数として保存されている必要があります。
AWS_DEFAULT_REGION: AWS リージョン。
これらの変数は、デプロイ環境、リポジトリ、またはワークスペース レベルで定義できます。
基本的な例: Lambda への Javascript 関数のデプロイ
Lambda 関数を更新するには、2 つの手順が必要です。
圧縮された関数コードをビルドする。
zip 形式のコードを AWS にデプロイする。
この例では、単純な node.js ベースの Lambda 関数を構築します。
次のコンテンツを使用して、index.jsファイルを作成します:
index.jsexports.handler = async (event) => { const response = { statusCode: 200, body: JSON.stringify('Hello world') }; return response; };bitbucket-pipelines.ymlファイルをアップデートします。次の 2 つのセクションがあります。圧縮された関数コードをビルドする
更新されたコードを AWS にプッシュする
以下の例では、
FUNCTION_NAME変数を関数の名前に置き換えます。
bitbucket-pipelines.yml の例
- step:
name: Build and package
script:
- apt-get update && apt-get install -y zip
- zip code.zip index.js
artifacts:
- code.zip
- step:
name: Update Lambda code
script:
- pipe: atlassian/aws-lambda-deploy:0.2.1
variables:
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}
AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION}
FUNCTION_NAME: 'my-function'
COMMAND: 'update'
ZIP_FILE: 'code.zip'高度な例: 複数の環境でエイリアスを使用する
AWS provides the ability to associate aliases with a particular version of a Lambda function. When you use them with aliases that represent the name of a deployment environment in Bitbucket Pipelines, you can promote versions of your functions through test, staging and production environments.
前の例に続けて、最初の 2 ステップを組み合わせ、テスト、ステージング、および本番環境を通じて関数をプロモートするステップを追加しています。
pipelines:
default:
- step:
# Build and package the Lambda function.
name: Build and package
script:
- apt-get update && apt-get install -y zip
- zip code.zip index.js
# Upload the Lambda - make the version number
#available to subsequent steps via artifacts.
- pipe: atlassian/aws-lambda-deploy:0.2.1
variables:
AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}
AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION}
FUNCTION_NAME: 'my-function'
COMMAND: 'update'
ZIP_FILE: 'code.zip'
# The pipe exports the newly published
# Lambda version to a file.
artifacts:
- pipe.meta.env
# You can optionally use AWS Lambda aliases
# to map the newly published Lambda
# function version to conceptual environments.
- step:
name: Deploy to Test
deployment: test
script:
# Read the 'function_version' from
# the update pipe into environment variables.
- source pipe.meta.env
# Point the test alias to the function.
- pipe: atlassian/aws-lambda-deploy:0.2.1
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
FUNCTION_NAME: 'my-function'
COMMAND: 'alias'
ALIAS: 'test'
VERSION: '${function_version}'
- step:
name: Deploy to Staging
deployment: staging
script:
# Read the 'function_version' from
# the update pipe into environment variables.
- source pipe.meta.env
# Point the 'staging' alias to the function.
- pipe: atlassian/aws-lambda-deploy:0.2.1
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
FUNCTION_NAME: 'my-function'
COMMAND: 'alias'
ALIAS: 'staging'
VERSION: '${function_version}'
- step:
name: Deploy to Production
deployment: production
script:
# Read the 'function_version' from
# the update pipe into environment variables.
- source pipe.meta.env
# Point the 'production' alias to the function.
- pipe: atlassian/aws-lambda-deploy:0.2.1
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
FUNCTION_NAME: 'my-function'
COMMAND: 'alias'
ALIAS: 'production'
VERSION: '${function_version}'この内容はお役に立ちましたか?