首页 > 解决方案 > 创建 AWS Lambda 函数时使用现有 IAM 角色

问题描述

我正在使用 CloudFormation 创建一个 lambda 函数。大多数文档都假定角色将在模板中创建。有没有办法指定已经通过控制台创建的角色?这个问题解决了一个类似的问题,但对于 EC2 实例创建:Associate existing IAM role with EC2 instance in CloudFormation

我正在寻找类似的东西:

 "LambdaFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "FunctionName": "My Function"
                "Runtime": "netcoreapp2.0",
                "Handler": "handler.location",
                "Role": "Existing_Role"

标签: amazon-web-servicesaws-lambdaamazon-cloudformation

解决方案


如果您参考云形成文档,

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html

您可以找到 Role 属性来替换您的角色。

它需要 arn 格式,而不仅仅是角色名。

arn:aws:iam::554668579590:role/ProdAdmin

"FunctionName": {
  "Type": "AWS::Lambda::Function",
  "Properties": {
    "Handler": "index.handler",
    "Role": "arn:aws:iam::AccountID:role/RoleName",
    "Code": {
      "S3Bucket": "lambda-functions",
      "S3Key": "amilookup.zip"
    },
    "Runtime": "nodejs4.3",
    "Timeout": 25,
    "TracingConfig": {
      "Mode": "Active"
   }
  }
}

推荐阅读