Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify lambda function and IAM role name in cloudformation template

here is my template

{  
   "AWSTemplateFormatVersion":"2010-09-09",
   "Resources":{  
      "lambdafunction":{  
         "Type":"AWS::Lambda::Function",
         "Properties":{  
            "Handler":"index.handler",
            "Role":{  
               "Fn::GetAtt":[  
                  "RootRole",
                  "Arn"
               ]
            },
            "Code":{  
               "S3Bucket":"{s3_bucket_name}",
               "S3Key":"lambda-zip"
            },
            "Runtime":"java8",
            "Timeout":"25"
         }
      },
      "RootRole":{  
         "Type":"AWS::IAM::Role",
         "Properties":{  
            "AssumeRolePolicyDocument":{  
               "Version":"2012-10-17",
               "Statement":[  
                  {  
                     "Effect":"Allow",
                     "Principal":{  
                        "Service":[  
                           "ec2.amazonaws.com"
                        ]
                     },
                     "Action":[  
                        "sts:AssumeRole"
                     ]
                  }
               ]
            },
            "Path":"/",
            "Policies":[  
               {  
                  "PolicyName":"root",
                  "PolicyDocument":{  
                     "Version":"2012-10-17",
                     "Statement":[  
                        {  
                           "Effect":"Allow",
                           "Action":"*",
                           "Resource":"*"
                        }
                     ]
                  }
               }
            ]
         }
      }
   }
}

The name of the lambda function after the stack creation is lambda-lambdafunction-18SJKJ5Q40AKZ The name of IAM role is lambda-RootRole-12S8E9CA0EOVM

The template does not seem to have a way to define the lambda function name http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html. And I am not sure why random characters are appended at the end.

like image 430
premprakash Avatar asked Sep 07 '15 22:09

premprakash


People also ask

How do you attach IAM role to Lambda function?

Attach the IAM policy to an IAM roleNavigate to the IAM console and choose Roles in the navigation pane. Choose Create role. Choose AWS service and then choose Lambda. Choose Next: Permissions.

How do you declare a Lambda function in a CloudFormation template?

The AWS::Lambda::Function resource creates a Lambda function. To create a function, you need a deployment package and an execution role. The deployment package is a . zip file archive or container image that contains your function code.

How do you assume a role in CloudFormation?

We'll need to create a role for the CloudFormation service to assume. That role will need a policy with the s3:CreateBucket permission. It also will need something called an assume role policy document which defines the trust relationship so that the CloudFormation service can assume this role.

What IAM role does CloudFormation use?

A service role is an AWS Identity and Access Management (IAM) role that allows AWS CloudFormation to make calls to resources in a stack on your behalf.


1 Answers

Update: both AWS::IAM::Role and AWS::Lambda::Function now support custom names.

By default, CloudFormation generates a unique ID for resource names. This makes sense because it allows you to re-use the template again and again.

Some resource types, but not all, support custom names. Examples that do support custom names are AWS::DynamoDB::Table ('TableName') and AWS::S3::Bucket ('BucketName').

For more info, and a complete list of resources that support custom names, see here.

like image 158
jarmod Avatar answered Sep 22 '22 15:09

jarmod