简体   繁体   English

有没有办法用 cloudformation 创建 aws lambda 执行角色?

[英]Is there a way to create aws lambda execution role with cloudformation?

I'm trying to create a lambda fuction with cloudformation but it requires a lambda execution role - is there a way I can generate one using cloudformation?我正在尝试使用 cloudformation 创建一个 lambda 功能,但它需要一个 lambda 执行角色 - 有没有一种方法可以使用 cloudformation 生成一个功能?

Yes, CloudFormation can be used to create an IAM role.是的,CloudFormation 可用于创建 IAM 角色。 The lambda execution role is an IAM role like any other IAM role. lambda 执行角色是一个 IAM 角色,就像任何其他 IAM 角色一样。 The documentation for doing so shows this example:这样做的文档显示了这个例子:

MyRole:
  Type: AWS::IAM::Role
  Properties: 
    AssumeRolePolicyDocument: Json
    Description: String
    ManagedPolicyArns: 
      - String
    MaxSessionDuration: Integer
    Path: String
    PermissionsBoundary: String
    Policies: 
      - Policy
    RoleName: String
    Tags: 
      - Tag

Then in the lambda, you reference it using a ref to the name of the role resource.然后在 lambda 中,使用对角色资源名称的引用来引用它。 Ex:前任:

  MyLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Role: !Ref MyRole
  

You can create an IAM role with a role policy where it will take region and account id from predefined AWS FloudFormation variables and assign it to lambda elements in cloud formation.您可以创建一个具有角色策略的 IAM 角色,它将从预定义的 AWS FloudFormation 变量中获取区域和账户 ID,并将其分配给云形成中的 lambda 元素。 please refer following example请参考以下示例

"Resources": {
    "AheadLambdaRole": {
        "Type": "AWS::IAM::Role",
        "Properties": {
            "RoleName": {
                "Fn::Sub": "AHEADLambdaRole-${EnvName}"
            },
            "AssumeRolePolicyDocument": {
                "Statement": [
                    {
                        "Action": [
                            "sts:AssumeRole"
                        ],
                        "Effect": "Allow",
                        "Principal": {
                            "Service": [
                                "lambda.amazonaws.com"
                            ]
                        }
                    }
                ],
                "Version": "2012-10-17"
            },
            "Policies": [{
                    "PolicyDocument" : {
                        "Version": "2012-10-17",
                        "Statement": [
                            {
                                "Effect": "Allow",
                                "Action": "logs:CreateLogGroup",
                                "Resource": {
                                    "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*"
                                }
                            },
                            {
                                "Effect": "Allow",
                                "Action": [
                                    "logs:CreateLogStream",
                                    "logs:PutLogEvents"
                                ],
                                "Resource": [
                                    { "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/LambdaName:*"}
                                ]
                            }
                        ]
                    },
                    "PolicyName" : "NameOfInlinepolicy"
                  }] 
         "ManagedPolicyArns": [
                "arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess",
                "arn:aws:iam::aws:policy/AmazonSSMFullAccess"
            ],
            "Path": "/"
        }
    }}

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

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