简体   繁体   English

AWS CustomeResource 卡在更新过程中

[英]AWS CustomeResource stuck at update in process

I have tried to use the lambda function to fetch some value from the SSM and supply to it through the EC2 at instance creation through UserData.我尝试使用 lambda function 从 SSM 获取一些值,并在通过 UserData 创建实例时通过 EC2 提供给它。 I have stuck at the process as it is Stuck at the CREATE_IN_PROGRESS stage.我一直停留在这个过程中,因为它停留在 CREATE_IN_PROGRESS 阶段。 Lambda function is created and it returns the value that i want when i run test through amazon console but customeresource is stuck. Lambda function 已创建,当我通过亚马逊控制台运行测试但 customeresource 卡住时,它返回我想要的值。

    Type: AWS::Lambda::Function
    Properties:
      FunctionName: "GetKeyFunction"
      Handler: "index.handler"
      Runtime: "python3.6"
      Timeout: 5
      Role: !GetAtt LamdaExecutionRole.Arn
      Code:
        ZipFile: |
          import boto3
          
          def handler(event, context):
            ssm = boto3.client('ssm')
            response = ssm.get_parameter(Name='private_key', WithDecryption=True)
            key = response['Parameter']['Value']
            return key
    
  KeyCustomeResource:
    Type: Custom::LamdaInvoker
    DependsOn: LambdaFunction
    Properties:
      ServiceToken: !GetAtt LamdaFunction.Arn

But the KeyCustomeResource is stuck at CREATE_IN_PROGRESS.但是 KeyCustomeResource 卡在 CREATE_IN_PROGRESS。 I am pretty new to python and AWS both.我对 python 和 AWS 都很陌生。 I cannot figure what is missing in it?我无法弄清楚其中缺少什么?

Thank You谢谢你

Your custom resource is stuck because Cloudformation does not know if it is deployed successfully or not.您的自定义资源卡住了,因为 Cloudformation 不知道它是否部署成功。 It will eventually timeout and you'll be able to delete it manually.它最终会超时,您将能够手动删除它。

To work correctly, in your lambda function you need to return a SUCCESS or FAILED status to Cloudformation and your 'key' in the Data field like this:要正常工作,在您的 lambda function 中,您需要向 Cloudformation 返回 SUCCESS 或 FAILED 状态,并在 Data 字段中返回“密钥”,如下所示:

response_data = {
    'Status': 'SUCCESS',
    'StackId': event['StackId'],
    'RequestId': event['RequestId'],
    'LogicalResourceId': event['LogicalResourceId'],
    'PhysicalResourceId': str(uuid.uuid4()) if event['RequestType'] == 'Create' else event['PhysicalResourceId'],
    'Data': {
        'Key': response['Parameter']['Value']
    }
}

return response_data

Similarly you'll need to send a Status:FAILED and a ['Data']['Reason'] if lambda fails or exceptions out - to help Cloudformation rollback or delete smoothly同样,如果 lambda 失败或出现异常,您需要发送 Status:FAILED 和 ['Data']['Reason'] - 以帮助 Cloudformation 顺利回滚或删除

You can access your 'key' in your template using a Cloudformation GetAtt like so: PrivateKey: .GetAtt KeyCustomeResource.Key您可以使用 Cloudformation GetAtt 访问模板中的“密钥”,如下所示: PrivateKey: .GetAtt KeyCustomeResource.Key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM