手动删除SAM CloudFormation堆栈中的函数后找不到函数 [英] Function not found after manually deleting a function in a SAM CloudFormation stack

查看:97
本文介绍了手动删除SAM CloudFormation堆栈中的函数后找不到函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 sam deploy 部署lambda函数和API网关.它可以正常工作,但是在我通过AWS控制台手动删除了lambda函数之后却无法正常工作.我遇到以下错误:

I am using sam deploy to deploy lambda function and API gateway. It works fine but it doesn't work after I manually deleted the lambda function via AWS console. I got below error:

"ResourceStatusReason": "Function not found: 
  arn:aws:lambda:ap-southeast-2:286334053171:function:polaroid (Service: 
  AWSLambdaInternal; Status Code: 404; Error Code: ResourceNotFoundException;
  Request ID: b431cbfc-7772-11e9-8022-1b92fa2cfa9e)

删除lambda并进行刷新部署的正确方法是什么?如果发生这种情况,如何强制SAM创建缺少的lambda函数?

What is the proper way to delete the lambda and do a refresh deployment? If this happens, how can I force SAM to create the missing lambda function?

我在模板yaml中的lambda看起来像:

My lambda in template yaml looks like:

...
Resources:
  PolaroidFunction:
    Type: AWS::Serverless::Function 
    Properties:
      FunctionName: test
      CodeUri: ./lambdas
      Handler: lib/index.fun
      Runtime: nodejs8.10
      Events:
        polaroid:
          Type: Api 
          Properties:
            Path: /test
            Method: post
...

推荐答案

我想您已经了解了绝不应该手动删除由SAM或CloudFormation管理的资源的困难方法.

I guess you already learnt the hard way that you should never manually delete resources managed by SAM or CloudFormation.

通常,如果只想更改功能,则只需调用 sam build sam deploy ,就会部署新版本.无需删除任何内容.如果您需要更高级的工作流程,则需要阅读博客文章.没有正确的方法来做到这一点.

In general, if you just want to change the function, you can just call sam build and sam deploy, and the new version of it will be deployed. There is no need to delete anything. If you need a more advanced workflow, you will need to read blog posts. There is no one right way to do this.

但是,要解决您的迫在眉睫的问题,这是您可以做的. 1

To fix your immediate problem however, here is what you can do.1

首先,您需要获取生成的AWS CloudFormation模板:

Firstly, you need to get the generated AWS CloudFormation template:

▶ aws cloudformation get-template --stack-name HelloWorld \
    --template-stage Processed --query TemplateBody | cfn-flip -y > processed.yml

接下来,您需要注释掉刚创建的 processed.yml 文件中的函数,并注释掉引用该函数的Lambda权限.还要保存原始 processed.yml 文件的备份.

Next, you need to comment out the function in the processed.yml file you just created, and also comment out the Lambda Permissions that refer to it. Save a backup of the original processed.yml file too.

此外,如果可能的话,通过从AWS控制台获取构建时计算出的实际值CloudFormation来更新对它的任何其他模板引用.例如,如果您有对 $ {HelloWorldFunction.Arn} 的引用,则可能必须使用类似于 arn:aws:lambda:ap-southeast-2的字符串来更新模板中的这些引用:123456789012:function:HelloWorld-HelloWorldFunction-1NJGQI7GEAUM1 .

Also, update any other template references to it if possible with the actual values CloudFormation computed when you built the stack, by getting them from your AWS console. For example, if you had references to ${HelloWorldFunction.Arn} you might have to update those references in the template with a string like arn:aws:lambda:ap-southeast-2:123456789012:function:HelloWorld-HelloWorldFunction-1NJGQI7GEAUM1.

接下来,使用AWS CloudFormation命令验证模板:

Next, validate the template using AWS CloudFormation commands:

▶ aws cloudformation validate-template --template-body file://processed.yml
{
    "CapabilitiesReason": "The following resource(s) require capabilities: [AWS::IAM::Role]",                                                                         
    "Description": "sam-app\nSample SAM Template for sam-app\n",
    "Parameters": [],
    "Capabilities": [
        "CAPABILITY_IAM"
    ]
}

接下来,您将使用此修改后的模板更新堆栈.通过这种方式更新堆栈,您可以从CloudFormation的角度使模板和真实状态恢复同步:

Next, you will update the stack using this modified template. By updating the stack this way, you get your template and real state to be back in sync from CloudFormation's point of view:

▶ aws cloudformation update-stack --template-body file://processed.yml --stack-name HelloWorld --capabilities CAPABILITY_IAM                        
{
    "StackId": "arn:aws:cloudformation:ap-southeast-2:885164491973:stack/HelloWorld/af2c6810-7884-11e9-9bb3-068b1a8e1450"
}

如果一切顺利,您的堆栈将进入UPDATE_COMPLETE状态.太好了!

If all goes well, your stack goes into UPDATE_COMPLETE state. Great!

最后,取消注释掉您注释掉的所有资源,并恢复所有原始值.然后第二次更新堆栈,您的堆栈应恢复到其原始状态.

Finally, uncomment all the resources you commented out, and restore all the original values. Then update stack a second time, and your stack should be restored to its original state.

另请参阅:

  • AWS Knowledge Base, 2016, 2019, How do I update an AWS CloudFormation stack that's failing because of a resource that I manually deleted?.
  • More on the cfn-flip utility, if you don't have it.

1 请注意,我使用SAM随附的默认HelloWorld Python 2.7示例测试了此方法.

1 Note that I tested this method using the default HelloWorld Python 2.7 example that ships with SAM.

这篇关于手动删除SAM CloudFormation堆栈中的函数后找不到函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆