Request: AWS::Serverless::Function example that includes a custom IAM Policy
Hello,
I cannot seem to create a functional example that does, in fact, create a lambda function with a custom IAM policy. This is a request to provide an example that works.
Non Working Example:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
TweetCollectionFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: test.handler
Runtime: python2.7
CodeUri: ./deployment.zip
MemorySize: 128
Timeout: 3
Policies:
- Effect: Allow
Action: s3:GetObject
Resource: '*'
This results in a lambda function that only includes 'AWSLambdaBasicExecutionRole'. I would expect it to also include s3:GetObject access on * resources. Right?
You need to specify the policy this this format:
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Resources:
TweetCollectionFunction:
Type: "AWS::Serverless::Function"
Properties:
Handler: test.handler
Runtime: python2.7
CodeUri: ./deployment.zip
MemorySize: 128
Timeout: 3
Policies:
-
Statement:
Effect: Allow
Action: "s3:GetObject"
Resource: "*"
Policies should be a policy document object and it should have a 'Statement'. Reference: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-policy.html#d0e81659
Thanks @vikrambhatt, that totally works. I was looking at this documentation, which is wrong, https://docs.aws.amazon.com/lambda/latest/dg/with-ct-example-use-app-spec.html - can you submit a bug report for this doc or shall I?
The doc looks correct. You were missing the "Statement" key in your template. Accepted formats are:
Either like this for single statement:
Policies:
Statement:
....
Or, like this for multiple statements:
Policies:
-
Statement:
....
Ok, thanks for the feedback. In hindsight, I properly just overlooked something. Exciting work, Thanks! 👍
Is it advisable to also specify Version: 2012-10-17? If I do, the stack creation fails saying Type of property 'Policies' is invalid. for a single Statement, as in
Policies:
Version: 2012-10-17
Statement:
- ...
Yeah, you don't have to specify the version. We will autopopulate it for you