serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

Request: AWS::Serverless::Function example that includes a custom IAM Policy

Open jolexa opened this issue 9 years ago • 6 comments

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?

jolexa avatar Jan 23 '17 00:01 jolexa

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

vikrambhatt avatar Jan 23 '17 01:01 vikrambhatt

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?

jolexa avatar Jan 23 '17 02:01 jolexa

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:
        ....

sanathkr avatar Jan 26 '17 20:01 sanathkr

Ok, thanks for the feedback. In hindsight, I properly just overlooked something. Exciting work, Thanks! 👍

jolexa avatar Jan 26 '17 20:01 jolexa

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:
    - ...

dinvlad avatar Jan 30 '17 23:01 dinvlad

Yeah, you don't have to specify the version. We will autopopulate it for you

sanathkr avatar Feb 02 '17 21:02 sanathkr