如何在 'sam package/deploy' 的运行时指定 AutoPublishAlias?
How to specify AutoPublishAlias at runtime of 'sam package/deploy'?
我将我们的 AWS SAM 部署包装在 Jenkins 中作为我们 CI/CD 管道的一部分。例如,我只想在合并时将 "live" 别名添加到 lambda,但我希望 "branch builds" 没有别名。这允许开发人员在 AWS 中测试代码而无需 "live"。除了在 运行 "sam package/deploy" 之前用 sed 替换 template.yaml 的一部分,还有其他方法可以做到这一点吗?
--更新--
看起来我可以使用参数在我的 lambda 中创建环境,但我不知道如何在它们之间切换。这看起来像:
Parameters:
MyEnv:
Description: Environment of this stack of resources
Type: String
Default: testing
AllowedValues:
- testing
- prod
然后我可以参考这个 w/:
Environment:
Variables:
ENV: !Ref: MyEnv
如果有人知道如何在 运行 时切换此参数,那将解决我的问题。
我成功了。我的 template.yaml:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sams-app
Globals:
Function:
Timeout: 3
Parameters:
Stage:
Type: String
Description: Which stage the code is in
Default: test
AllowedValues:
- test
- prod
Resources:
HelloWorldSQSFunction:
Type: AWS::Serverless::Function
Properties:
Role: arn:aws:iam::xxxxxxxxxxxx:role/service_lambda_default1
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.7
AutoPublishAlias: !Ref Stage
DeploymentPreference:
Type: AllAtOnce
Environment:
Variables:
STAGE: !Ref Stage
Outputs:
HelloWorldSQSFunction:
Description: "Hello World SQS Lambda Function ARN"
Value: !GetAtt HelloWorldSQSFunction.Arn
我的 lambda 代码:
import json
import os
def lambda_handler(event, context):
stage = os.environ['STAGE']
print(f"My stage is: {stage}")
return {
"statusCode": 200,
}
然后 运行 在本地(我使用的是 Cloud9):
DEVELOPER:~/environment/sams-app $ sam local invoke --parameter-overrides Stage=prod
Invoking app.lambda_handler (python3.7)
Fetching lambci/lambda:python3.7 Docker container image......
Mounting /home/ec2-user/environment/sams-app/hello_world as /var/task:ro,delegated inside runtime container
START RequestId: 85da81b1-ef74-1b7d-6ad0-a356f4aa8b76 Version: $LATEST
My stage is: prod
END RequestId: 85da81b1-ef74-1b7d-6ad0-a356f4aa8b76
REPORT RequestId: 85da81b1-ef74-1b7d-6ad0-a356f4aa8b76 Init Duration: 127.56 ms Duration: 3.69 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 22 MB
{"statusCode":200}
需要注意的一件事是,这会导致您的 "sam validate" 失败。有关这方面的信息,请参阅:https://github.com/awslabs/serverless-application-model/issues/778
特别感谢JLarky for the comment on this thread: aws-sam-local environment variables
我将我们的 AWS SAM 部署包装在 Jenkins 中作为我们 CI/CD 管道的一部分。例如,我只想在合并时将 "live" 别名添加到 lambda,但我希望 "branch builds" 没有别名。这允许开发人员在 AWS 中测试代码而无需 "live"。除了在 运行 "sam package/deploy" 之前用 sed 替换 template.yaml 的一部分,还有其他方法可以做到这一点吗?
--更新-- 看起来我可以使用参数在我的 lambda 中创建环境,但我不知道如何在它们之间切换。这看起来像:
Parameters:
MyEnv:
Description: Environment of this stack of resources
Type: String
Default: testing
AllowedValues:
- testing
- prod
然后我可以参考这个 w/:
Environment:
Variables:
ENV: !Ref: MyEnv
如果有人知道如何在 运行 时切换此参数,那将解决我的问题。
我成功了。我的 template.yaml:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sams-app
Globals:
Function:
Timeout: 3
Parameters:
Stage:
Type: String
Description: Which stage the code is in
Default: test
AllowedValues:
- test
- prod
Resources:
HelloWorldSQSFunction:
Type: AWS::Serverless::Function
Properties:
Role: arn:aws:iam::xxxxxxxxxxxx:role/service_lambda_default1
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.7
AutoPublishAlias: !Ref Stage
DeploymentPreference:
Type: AllAtOnce
Environment:
Variables:
STAGE: !Ref Stage
Outputs:
HelloWorldSQSFunction:
Description: "Hello World SQS Lambda Function ARN"
Value: !GetAtt HelloWorldSQSFunction.Arn
我的 lambda 代码:
import json
import os
def lambda_handler(event, context):
stage = os.environ['STAGE']
print(f"My stage is: {stage}")
return {
"statusCode": 200,
}
然后 运行 在本地(我使用的是 Cloud9):
DEVELOPER:~/environment/sams-app $ sam local invoke --parameter-overrides Stage=prod
Invoking app.lambda_handler (python3.7)
Fetching lambci/lambda:python3.7 Docker container image......
Mounting /home/ec2-user/environment/sams-app/hello_world as /var/task:ro,delegated inside runtime container
START RequestId: 85da81b1-ef74-1b7d-6ad0-a356f4aa8b76 Version: $LATEST
My stage is: prod
END RequestId: 85da81b1-ef74-1b7d-6ad0-a356f4aa8b76
REPORT RequestId: 85da81b1-ef74-1b7d-6ad0-a356f4aa8b76 Init Duration: 127.56 ms Duration: 3.69 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 22 MB
{"statusCode":200}
需要注意的一件事是,这会导致您的 "sam validate" 失败。有关这方面的信息,请参阅:https://github.com/awslabs/serverless-application-model/issues/778
特别感谢JLarky for the comment on this thread: aws-sam-local environment variables