解决AWS Codedeploy挂钩中的状态回调

时间:2019-07-05 10:34:00

标签: aws-lambda amazon-iam aws-code-deploy

我在运行代码部署时收到此错误

enter image description here

这是我的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"
                    ]
                }
            }
        }
    ]
}

我确实有一个基于此link的返回回调的状态代码,但它似乎不起作用。那么codedeploy接受哪种回调?

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。我需要在run_task语句后显式调用codedeploy.putLifecycleEventHookExecutionStatus

所以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