使用容器镜像添加 Amazon AppConfig 代理 Lambda 扩展 - Amazon AppConfig
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用容器镜像添加 Amazon AppConfig 代理 Lambda 扩展

您可以将您的 A Amazon AppConfig gent Lambda 扩展打包为容器映像,然后将其上传到托管在亚马逊弹性容器注册表 (Amazon ECR) Container Registry (Amazon ECR) 上的容器注册表。

将 Lambda Amazon AppConfig 代理扩展添加为 Lambda 容器镜像
  1. 在 () 中输入 Amazon Web Services 区域 和亚马逊资源名称 (ARN), Amazon Command Line Interface 如下所示。Amazon CLI将区域和 ARN 值替换为您的地区和匹配的 ARN,以检索 Lambda 层的副本。 Amazon AppConfig x86-64 和 ARM64 平台提供 ARN。

    aws lambda get-layer-version-by-arn \ --region Amazon Web Services 区域 \ --arn extension ARN

    以下为示例。

    aws lambda get-layer-version-by-arn \ --region us-east-1 \ --arn arn:aws:lambda:us-east-1:027255383542:layer:AWS-AppConfig-Extension:128

    响应看起来与以下内容类似:

    { "LayerVersionArn": "arn:aws:lambda:us-east-1:027255383542:layer:AWS-AppConfig-Extension:128", "Description": "Amazon AppConfig extension: Use dynamic configurations deployed via Amazon AppConfig for your Amazon Lambda functions", "CreatedDate": "2021-04-01T02:37:55.339+0000", "LayerArn": "arn:aws:lambda::layer:Amazon-AppConfig-Extension", "Content": { "CodeSize": 5228073, "CodeSha256": "8otOgbLQbexpUm3rKlMhvcE6Q5TvwcLCKrc4Oe+vmMY=", "Location" : "S3-Bucket-Location-URL" }, "Version": 30, "CompatibleRuntimes": [ "python3.8", "python3.7", "nodejs12.x", "ruby2.7" ], }
  2. 在上面的响应中,为 Location 返回的值是包含 Lambda 扩展的 Amazon Simple Storage Service (Amazon S3) 存储桶的 URL。将 URL 粘贴到 Web 浏览器中,以便下载 Lambda 扩展 .zip 文件。

    注意

    Amazon S3 存储桶 URL 仅在 10 分钟内可用。

    (可选)或者,您也可以使用以下 curl 命令下载 Lambda 扩展。

    curl -o extension.zip "S3-Bucket-Location-URL"

    (可选)或者,您可以结合步骤 1步骤 2 来检索 ARN 并一次下载 .zip 扩展名文件。

    aws lambda get-layer-version-by-arn \ --arn extension ARN \ | jq -r '.Content.Location' \ | xargs curl -o extension.zip
  3. Dockerfile 中添加以下行,将扩展添加到容器映像。

    COPY extension.zip extension.zip RUN yum install -y unzip \ && unzip extension.zip /opt \ && rm -f extension.zip
  4. 确保 Lambda 函数执行角色具有 appconfig: GetConfiguration 权限集

示例

本节包括一个在基于容器映像的 Python Lambda 函数上启用 A Amazon AppConfig gent Lambda 扩展的示例。

  1. 创建一个类似于以下操作的 Dockerfile

    FROM public.ecr.aws/lambda/python:3.8 AS builder COPY extension.zip extension.zip RUN yum install -y unzip \ && unzip extension.zip -d /opt \ && rm -f extension.zip FROM public.ecr.aws/lambda/python:3.8 COPY --from=builder /opt /opt COPY index.py ${LAMBDA_TASK_ROOT} CMD [ "index.handler" ]
  2. 将扩展层下载到与 Dockerfile 相同的目录。

    aws lambda get-layer-version-by-arn \ --arn extension ARN \ | jq -r '.Content.Location' \ | xargs curl -o extension.zip
  3. 在与 Dockerfile 相同的目录中创建名为 index.py 的 Python 文件。

    import urllib.request def handler(event, context): return { # replace parameters here with your application, environment, and configuration names 'configuration': get_configuration('myApp', 'myEnv', 'myConfig'), } def get_configuration(app, env, config): url = f'http://localhost:2772/applications/{app}/environments/{env}/configurations/{config}' return urllib.request.urlopen(url).read()
  4. 然后,在该目录中执行以下步骤来构建 docker 镜像并将其上载到 Amazon ECR。

    // set environment variables export ACCOUNT_ID = <YOUR_ACCOUNT_ID> export REGION = <AWS_REGION> // create an ECR repository aws ecr create-repository --repository-name test-repository // build the docker image docker build -t test-image . // sign in to AWS aws ecr get-login-password \ | docker login \ --username AWS \ --password-stdin "$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com" // tag the image docker tag test-image:latest "$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/test-repository:latest" // push the image to ECR docker push "$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/test-repository:latest"
  5. 使用您刚刚创建的 Amazon ECR 镜像以创建 Lambda 函数。有关作为容器的 Lambda 函数的更多信息,请参阅创建定义为容器映像的 Lambda 函数

  6. 确保 Lambda 函数执行角色具有 appconfig: GetConfiguration 权限集