Lambda 関数の更新を AWS にデプロイする
この内容はお役に立ちましたか?
exports.handler = async (event) => {\n const response = {\n statusCode: 200,\n body: JSON.stringify(\'Hello world\')\n };\n return response;\n};
Update the bitbucket-pipelines.yml
file. There are two sections:
build the zipped up function code
更新されたコードを AWS にプッシュする
以下の例では、FUNCTION_NAME
変数を関数の名前に置き換えます。
bitbucket-pipelines.yml の例
- step:\n name: Build and package\n script:\n - apt-get update && apt-get install -y zip\n - zip code.zip index.js\n artifacts:\n - code.zip\n- step:\n name: Update Lambda code\n script:\n - pipe: atlassian/aws-lambda-deploy:0.2.1\n variables:\n AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}\n AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}\n AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION}\n FUNCTION_NAME: \'my-function\'\n COMMAND: \'update\'\n ZIP_FILE: \'code.zip\'
AWS では、エイリアスを特定のバージョンの Lambda 関数に関連付ける機能を提供しています。Bitbucket Pipeline のデプロイ環境の名前を表すエイリアスとともに使用すると、テスト、ステージング、本番環境を通じて関数のバージョンをプロモートできます。
前の例に続けて、最初の 2 ステップを組み合わせ、テスト、ステージング、および本番環境を通じて関数をプロモートするステップを追加しています。
pipelines:\n default:\n - step:\n # Build and package the Lambda function.\n name: Build and package\n script:\n - apt-get update && apt-get install -y zip\n - zip code.zip index.js\n\n # Upload the Lambda - make the version number \n #available to subsequent steps via artifacts.\n - pipe: atlassian/aws-lambda-deploy:0.2.1\n variables:\n AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}\n AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}\n AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION}\n FUNCTION_NAME: \'my-function\'\n COMMAND: \'update\'\n ZIP_FILE: \'code.zip\'\n\n # The pipe exports the newly published \n # Lambda version to a file.\n artifacts:\n - pipe.meta.env\n\n # You can optionally use AWS Lambda aliases \n # to map the newly published Lambda\n # function version to conceptual environments.\n - step:\n name: Deploy to Test\n deployment: test\n script:\n # Read the \'function_version\' from \n # the update pipe into environment variables.\n - source pipe.meta.env\n # Point the test alias to the function.\n - pipe: atlassian/aws-lambda-deploy:0.2.1\n variables:\n AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID\n AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY\n AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION\n FUNCTION_NAME: \'my-function\'\n COMMAND: \'alias\'\n ALIAS: \'test\'\n VERSION: \'${function_version}\'\n\n - step:\n name: Deploy to Staging\n deployment: staging\n script:\n # Read the \'function_version\' from \n # the update pipe into environment variables.\n - source pipe.meta.env\n # Point the \'staging\' alias to the function.\n - pipe: atlassian/aws-lambda-deploy:0.2.1\n variables:\n AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID\n AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY\n AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION\n FUNCTION_NAME: \'my-function\'\n COMMAND: \'alias\'\n ALIAS: \'staging\'\n VERSION: \'${function_version}\'\n\n - step:\n name: Deploy to Production\n deployment: production\n script:\n # Read the \'function_version\' from\n # the update pipe into environment variables.\n - source pipe.meta.env\n # Point the \'production\' alias to the function.\n - pipe: atlassian/aws-lambda-deploy:0.2.1\n variables:\n AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID\n AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY\n AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION\n FUNCTION_NAME: \'my-function\'\n COMMAND: \'alias\'\n ALIAS: \'production\'\n VERSION: \'${function_version}\'
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.
You need to have:
Access to the AWS console.
An existing Lambda function.
An IAM user with sufficient permissions and access to update the Lambda function
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.
We recommend attaching the AWSLambdaFullAccess policy to the IAM user, to full permissions to work with Lambda functions.
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 user's AWS access key.
AWS_SECRET_ACCESS_KEY: the IAM user's AWS secret access key. Make sure that you save it as a secured variable.
AWS_DEFAULT_REGION: Your AWS region.
You can define these variables at the deployment environment, repository, or workspace level.
To update a Lambda function, 2 steps are required:
building the zipped up function code
deploying the zipped code to AWS.
In this example, we will build a simple node.js based Lambda function.
Create a file, index.js, with the following content:
index.js
exports.handler = async (event) => {\n const response = {\n statusCode: 200,\n body: JSON.stringify('Hello world')\n };\n return response;\n};
Update the bitbucket-pipelines.yml
file. There are two sections:
build the zipped up function code
push the updated code to AWS
In the example below replace the FUNCTION_NAME
variable with the name of your function.
Example bitbucket-pipelines.yml
- step:\n name: Build and package\n script:\n - apt-get update && apt-get install -y zip\n - zip code.zip index.js\n artifacts:\n - code.zip\n- step:\n name: Update Lambda code\n script:\n - pipe: atlassian/aws-lambda-deploy:0.2.1\n variables:\n AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}\n AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}\n AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION}\n FUNCTION_NAME: 'my-function'\n COMMAND: 'update'\n 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.
Following on from the previous example, we've combined the first 2 steps, and now are adding on steps to promote the function through test, staging and production environments.
pipelines:\n default:\n - step:\n # Build and package the Lambda function.\n name: Build and package\n script:\n - apt-get update && apt-get install -y zip\n - zip code.zip index.js\n\n # Upload the Lambda - make the version number \n #available to subsequent steps via artifacts.\n - pipe: atlassian/aws-lambda-deploy:0.2.1\n variables:\n AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}\n AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY}\n AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION}\n FUNCTION_NAME: 'my-function'\n COMMAND: 'update'\n ZIP_FILE: 'code.zip'\n\n # The pipe exports the newly published \n # Lambda version to a file.\n artifacts:\n - pipe.meta.env\n\n # You can optionally use AWS Lambda aliases \n # to map the newly published Lambda\n # function version to conceptual environments.\n - step:\n name: Deploy to Test\n deployment: test\n script:\n # Read the 'function_version' from \n # the update pipe into environment variables.\n - source pipe.meta.env\n # Point the test alias to the function.\n - pipe: atlassian/aws-lambda-deploy:0.2.1\n variables:\n AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID\n AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY\n AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION\n FUNCTION_NAME: 'my-function'\n COMMAND: 'alias'\n ALIAS: 'test'\n VERSION: '${function_version}'\n\n - step:\n name: Deploy to Staging\n deployment: staging\n script:\n # Read the 'function_version' from \n # the update pipe into environment variables.\n - source pipe.meta.env\n # Point the 'staging' alias to the function.\n - pipe: atlassian/aws-lambda-deploy:0.2.1\n variables:\n AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID\n AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY\n AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION\n FUNCTION_NAME: 'my-function'\n COMMAND: 'alias'\n ALIAS: 'staging'\n VERSION: '${function_version}'\n\n - step:\n name: Deploy to Production\n deployment: production\n script:\n # Read the 'function_version' from\n # the update pipe into environment variables.\n - source pipe.meta.env\n # Point the 'production' alias to the function.\n - pipe: atlassian/aws-lambda-deploy:0.2.1\n variables:\n AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID\n AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY\n AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION\n FUNCTION_NAME: 'my-function'\n COMMAND: 'alias'\n ALIAS: 'production'\n VERSION: '${function_version}'
この内容はお役に立ちましたか?