【问题标题】:Using existing IAM role when creating AWS Lambda Function创建 AWS Lambda 函数时使用现有 IAM 角色
【发布时间】:2023-04-10 12:50:01
【问题描述】:

我正在使用 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-services aws-lambda amazon-cloudformation


    【解决方案1】:

    如果您参考云形成文档,

    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"
       }
      }
    }
    

    【讨论】:

      【解决方案2】:

      要使用 CloudFormation StackSet 在多个帐户上运行,请使用 Fn::Sub 替换变量 - 在本例中,使用帐户 ID:

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

      【讨论】:

        【解决方案3】:

        这行得通 "角色": { "Fn::Sub": "arn:aws:iam::${AWS::AccountId}:role/RoleName" },

        【讨论】:

          猜你喜欢
          • 2021-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-29
          • 2019-12-08
          相关资源
          最近更新 更多