question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

iamRoleStatement/Resource with pseudo-parametes/intrinsic functions ${AWS::Region} and ${AWS::AccountId} creates invalid Cloudformation template

See original GitHub issue

This is a Bug Report

Description

Defining iamRoleStatements/Resource with pseudo-parameters/instrinsic functions ${AWS::Region} and ${AWS::AccountId} leads to invalid Cloudformation in cloudformation-template-update-stack.json See also http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html#cfn-pseudo-param-accountid

  • What went wrong? Deploying attached serverless.yml causes AWS error: “An error occurred while provisioning your stack: IamRoleLambdaExecution - The policy failed legacy parsing.”

Reason is that some policies are created as

"Resource": [
   "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/iamParsingTest-dev-hello:*:*"
]

instead of:

"Resource": [
  {
    "Fn::Sub": "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/iamParsingTest-dev-hello:*:*"
  }
]

The error only occurred when a function and a resource were defined as well.

  • What did you expect should have happened? Expected correct variable replacement and correct deployment

  • What was the config you used? See files below

  • What stacktrace or error message from your provider did you see? See above: “An error occurred while provisioning your stack: IamRoleLambdaExecution - The policy failed legacy parsing.”

Similar or dependent issues:

Additional Data

serverless.yml (derived from https://github.com/serverless/serverless/pull/3111):


## Workaround attempt:
custom:
  acc: ${AWS::AccountId}

provider:
  name: aws
  runtime: nodejs6.10
  region: us-east-1
  # https://serverless.com/framework/docs/providers/aws/guide/variables/
  variableSyntax: "\\${{([ :a-zA-Z0-9._,\\-\\/\\(\\)]+?)}}"
  stage:  dev
  
  iamRoleStatements:
    - Effect: "Allow"
      Action: 
        - "dynamodb:GetRecords"
        - "dynamodb:GetShardIterator"
        - "dynamodb:DescribeStream"
        - "dynamodb:ListStreams"
      Resource:
        # Next line causes "IamRoleLambdaExecution - The policy failed legacy parsing."
        #- "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/myTable/stream/*"
        ## Workaround does not work as ${AWS:AccountId} is not resolved, just passed on to CloudFormation without resolving it
        - "arn:aws:dynamodb:${{self:provider.region}}:${{self:custom.acc}}:table/myTable/stream/*"
        
# Note: error only occurs if function/resource is defined
functions:
  hello:
    handler: handler.hello
    events:
      - stream:
          type: dynamodb
          arn:
            Fn::GetAtt:
              - ResourcesDynamoDBStream
              - StreamArn
          batchSize: 1

resources:
  Resources:
    ResourcesDynamoDBStream:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ResourcesDynamoDBStream
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        StreamSpecification:
          StreamViewType: NEW_AND_OLD_IMAGES

handler.js (auto-generated):

'use strict';
module.exports.hello = (event, context, callback) => {
  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Go Serverless v1.0! Your function executed successfully!',
      input: event,
    }),
  };
  callback(null, response);
};
  • Serverless Framework Version you’re using: 1 .17.0
  • Operating System: Win10
  • Stack Trace: none
  • Provider Error messages: “An error occurred while provisioning your stack: IamRoleLambdaExecution - The policy failed legacy parsing.”

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
amphibithencommented, Dec 20, 2017

i experienced this today with iam policy as well when trying to reference ${AWS::Region} and ${AWS::AccountId} resulting in legacy parsing error during cloudformation load. work around was using !Join and !Ref, but that is a lot of extra that should not be needed i think…

0reactions
rickysahucommented, Apr 28, 2019

As others suggested, we fixed it by moving arn:aws:logs:${self:provider.region}:#{AWS::AccountId}:log-group/abc/* to arn:aws:logs:${self:provider.region}:*:log-group/abc/*

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve template validation or template format errors in ... - AWS
I receive an error message when I try to create my AWS CloudFormation ... For "Unrecognized parameter type: XXXXXXXX" or "Invalid template ......
Read more >
How do I resolve template validation or template format errors ...
Juntao shows you how to resolve template validation or template format errors in AWS CloudFormation. Subscribe: More AWS videos ...
Read more >
Deploy an AWS CloudFormation template
Octopus supports the deployment of AWS CloudFormation templates through the Deploy an AWS CloudFormation Template step. This step executes a ...
Read more >
2. CloudFormation Fundamentals - LinkedIn
AWS CloudFormation is the IaC tool offered by Amazon: we can create, update, delete our resources also known as a stack.
Read more >
PhysicalResourceId in CloudFormation - Innablr
Many of us use Cloudformation for deploying our infrastructure in AWS. ... Cloudformation stack, every resource you describe in the template ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found