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 では、IAM ユーザーをセットアップするためのチュートリアルと、AWS コンソールを使用して Lambda 関数を作成する方法のチュートリアルが提供されています。

AWSLambdaFullAccess ポリシーを IAM ユーザーに関連付け、Lambda 関数を操作するための完全な権限を付与することをおすすめします。

AWS 資格情報を Bitbucket Pipelines に追加する

Lambda 関数の更新に使用される IAM ユーザーの認証情報を含む、3 つの変数を Bitbucket Pipelines に追加する必要があります。

  • AWS_ACCESS_KEY_ID: IAM ユーザーの AWS アクセス キー。

  • AWS_SECRET_ACCESS_KEY: IAM ユーザーの AWS 秘密アクセス キー。セキュアな変数として保存されている必要があります。

  • AWS_DEFAULT_REGION: AWS リージョン。

You can define these variables at the deployment environment, repository, or workspace level.

基本的な例: Lambda への Javascript 関数のデプロイ

Lambda 関数を更新するには、2 つの手順が必要です。

  • 圧縮された関数コードをビルドする。

  • zip 形式のコードを AWS にデプロイする。

この例では、単純な node.js ベースの Lambda 関数を構築します。

  1. 次のコンテンツを使用して、index.jsファイルを作成します:

    index.js

    exports.handler = async (event) => { const response = { statusCode: 200, body: JSON.stringify('Hello world') }; return response; };
  2. Update the bitbucket-pipelines.yml file. There are two sections:

    1. build the zipped up function code

    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 では、エイリアスを特定のバージョンの Lambda 関数に関連付ける機能を提供しています。Bitbucket Pipeline のデプロイ環境の名前を表すエイリアスとともに使用すると、テスト、ステージング、本番環境を通じて関数のバージョンをプロモートできます。

前の例に続けて、最初の 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}'

さらにヘルプが必要ですか?

アトラシアン コミュニティをご利用ください。
OSZAR »