aws-lambda - 在 AWS codedeploy 挂钩中解析状态回调
问题描述
我在运行 codedeploy 时收到此错误
这是我的 appspec.yaml 文件
version: 0.0
Resources:
- TargetService:
Type: AWS::ECS::Service
Properties:
TaskDefinition: "arn:aws:ecs:ap-southeast-1:xxx:task-definition/xxxx-def:latest"
LoadBalancerInfo:
ContainerName: "yyyyy"
ContainerPort: 80
# Optional properties
PlatformVersion: "LATEST"
NetworkConfiguration:
AwsvpcConfiguration:
Subnets: ["subnet-xxx","subnet-yyy"]
SecurityGroups: ["sg-zzz"]
Hooks:
- BeforeInstall: "drush-updb"
这就是drush-updb
AWS lambda 中的作用
def lambda_handler(event,context):
client = boto3.client('ecs')
response = client.run_task(
overrides={
'containerOverrides': [
{
'name': 'AAA-BBB',
'command': [
"ccdd"
],
}
]
}
)
return {
'statusCode': 200,
'body': str(response)
}
最后是运行代码部署的 IAM。
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ecs:DescribeServices",
"ecs:CreateTaskSet",
"ecs:UpdateServicePrimaryTaskSet",
"ecs:DeleteTaskSet",
"elasticloadbalancing:DescribeTargetGroups",
"elasticloadbalancing:DescribeListeners",
"elasticloadbalancing:ModifyListener",
"elasticloadbalancing:DescribeRules",
"elasticloadbalancing:ModifyRule",
"lambda:InvokeFunction",
"cloudwatch:DescribeAlarms",
"sns:Publish",
"s3:*"
],
"Resource": "*",
"Effect": "Allow"
},
{
"Action": [
"iam:PassRole"
],
"Effect": "Allow",
"Resource": "*",
"Condition": {
"StringLike": {
"iam:PassedToService": [
"ecs-tasks.amazonaws.com"
]
}
}
}
]
}
我确实有基于此链接的返回回调的状态代码,但它似乎不起作用。那么 codedeploy 接受什么样的回调呢?
解决方案
我设法解决了这个问题。我需要codedeploy.putLifecycleEventHookExecutionStatus
在 run_task 语句之后显式调用。
所以 lambda 函数看起来像这样
def lambda_handler(event,context):
client = boto3.client('ecs')
response = client.run_task(
overrides={
'containerOverrides': [
{
'name': 'AAA-BBB',
'command': [
"ccdd"
],
}
]
}
)
if response:
status='Succeeded'
try:
codedeploy.put_lifecycle_event_hook_execution_status(
deploymentId=event["DeploymentId"],
lifecycleEventHookExecutionId=event["LifecycleEventHookExecutionId"],
status=status
)
return True
except ClientError as e:
print("Unexpected error: %s" % e)
return False
推荐阅读
- asp.net-mvc - 如何限制 Grid.MVC 中的分页大小
- jquery - 如何在另一个元素jquery的中心为可拖动元素设置动画
- android - TensorFlowInferenceInterface 返回低值
- ios - UIView transform.scaleBy 动画不起作用
- c++11 - 使用值作为结构数据类型更新 std::map
- javascript - 用php自动填充html表单
- f# - 检索报价中的 F# 代码
- javascript - 如何从 momentjs 对象中提取日期和时间并将它们加入形成新的时刻对象?
- javascript - 为什么这个循环在 99999 处停止
- wordpress - 将字段添加到自定义 WP 登录表单