aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

AWS::Serverless::Api not applying Tags to all generated resources, PropagateTags not defined.

Open cheriejw opened this issue 3 years ago • 3 comments

Description

I am using the AWS::Serverless::Api resource and attempting to have it tag the generated AWS::ApiGateway::RestApi resource. Currently only the generated AWS::ApiGateway::Stage resource receives the Tags provided in the template.yaml.

I looked at the documentation page for AWS::Serverless::Api and it looks like there is a PropagateTags property that can be set, that sounds like it will do what I want. I set the property, but I get a InvalidResourceException error.

Steps to reproduce

Set template.yaml to:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sam-app

Globals:
  Function:
    Tags: &tags
      testkey: testvalue
      env: testerstagename

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8
      Events:
        HelloWorld:
          Type: Api 
          Properties:
            Path: /helloWorld
            Method: get
        HttpPost:
          Type: Api
          Properties:
            RestApiId: !Ref QueryApi
            Path: /zendesk/query
            Method: post

  QueryApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: testerstagename
      Cors:
        AllowMethods: "'*'"
        AllowHeaders: "'*'"
        AllowOrigin: "'*'"
      MethodSettings:
        - ResourcePath: '/*'
          HttpMethod: '*'
      PropagateTags: True
      Tags: *tags

Outputs:
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

Note: This configuration is valid without the added PropagateTags value.

Run sam validate or sam deploy.

Observed result

Error: [InvalidResourceException('QueryApi', 'property PropagateTags not defined for resource of type AWS::Serverless::Api')] ('QueryApi', 'property PropagateTags not defined for resource of type AWS::Serverless::Api')

Expected result

I expect PropagateTags to be a valid property for AWS::Serverless::Api definition.

If it is not supported, then I do not expect to see it in the documentation page for AWS::Serverless::Api. Screenshot 2023-07-07 at 8 13 18 AM

Additional environment details

  1. OS: macOS Ventura - Version 13.4
  2. If using the SAM CLI sam --version: SAM CLI, version 1.89.0 aws --version: aws-cli/2.12.4 Python/3.11.4 Darwin/22.5.0 source/arm64 prompt/off
  3. AWS region: us-west-1

cheriejw avatar Jul 07 '23 15:07 cheriejw

Hi, PropagateTags was added to the SAM transform library in 1.71.0, but the latest SAM CLI depends on 1.70.0, which is why you're seeing that error. https://github.com/aws/aws-sam-cli/pull/5455 has been merged and it should work in the upcoming SAM CLI release.

For context, SAM consists of the AWS::Serverless-2016-10-31 transform (i.e. https://github.com/aws/serverless-application-model) and the SAM CLI (i.e. https://github.com/aws/aws-sam-cli). The transform works independently of the SAM CLI (e.g. you can deploy templates with AWS::Serverless-2016-10-31 without using SAM CLI), and we typically update documentation once the transform changes are released. However, the SAM CLI runs the SAM transform library locally for various purposes, which is why some SAM CLI commands fail when it uses an older version of the transform library.

hoffa avatar Jul 07 '23 17:07 hoffa

@hoffa That makes sense. I just started working with SAM so thank you so much for the helpful explanation + issue transfer!

cheriejw avatar Jul 10 '23 14:07 cheriejw

Patch is released in v1.91.0. Closing

github-actions[bot] avatar Jul 18 '23 18:07 github-actions[bot]