Skip to content

Commit 21823a3

Browse files
authored
fix: prevent redundant tags being added to AWS::ApiGatewayV2::Api (#3615)
1 parent c64c882 commit 21823a3

11 files changed

+374
-36
lines changed

samtranslator/model/apigatewayv2.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class ApiGatewayV2HttpApi(Resource):
2525
}
2626

2727
runtime_attrs = {"http_api_id": lambda self: ref(self.logical_id)}
28-
Tags: Optional[PassThrough]
2928

3029
def assign_tags(self, tags: Dict[str, Any]) -> None:
3130
"""Overriding default 'assign_tags' function in Resource class
@@ -34,8 +33,8 @@ def assign_tags(self, tags: Dict[str, Any]) -> None:
3433
:param tags: Tags to be assigned to the resource
3534
3635
"""
37-
if tags is not None and "Tags" in self.property_types:
38-
self.Tags = tags
36+
# Tags are already defined in Body so they do not need to be assigned here
37+
return
3938

4039

4140
class ApiGatewayV2Stage(Resource):
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Resources:
4+
MyHttpApi:
5+
Type: AWS::Serverless::HttpApi
6+
Properties:
7+
DefinitionBody:
8+
openapi: 3.0.1
9+
info:
10+
title: My API
11+
version: 1.0.0
12+
paths:
13+
/:
14+
get:
15+
x-amazon-apigateway-integration:
16+
type: aws_proxy
17+
httpMethod: POST
18+
uri:
19+
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyLambdaFunction.Arn}/invocations
20+
responses:
21+
'200':
22+
description: OK
23+
PropagateTags: true
24+
Tags:
25+
Project: MyProject
26+
27+
28+
MyLambdaFunction:
29+
Type: AWS::Serverless::Function
30+
Properties:
31+
Handler: hello.handler
32+
Runtime: python3.10
33+
CodeUri: s3://my-bucket/my-code.zip

tests/translator/output/aws-cn/function_with_events_and_propagate_tags.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,6 @@
564564
"x-amazon-apigateway-tag-value": "SAM"
565565
}
566566
]
567-
},
568-
"Tags": {
569-
"Key1": "Value1",
570-
"Key2": "Value2",
571-
"httpapi:createdBy": "SAM"
572567
}
573568
},
574569
"Type": "AWS::ApiGatewayV2::Api"
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"AWSTemplateFormatVersion": "2010-09-09",
3+
"Resources": {
4+
"MyHttpApi": {
5+
"Properties": {
6+
"Body": {
7+
"info": {
8+
"title": "My API",
9+
"version": "1.0.0"
10+
},
11+
"openapi": "3.0.1",
12+
"paths": {
13+
"/": {
14+
"get": {
15+
"responses": {
16+
"200": {
17+
"description": "OK"
18+
}
19+
},
20+
"x-amazon-apigateway-integration": {
21+
"httpMethod": "POST",
22+
"type": "aws_proxy",
23+
"uri": {
24+
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyLambdaFunction.Arn}/invocations"
25+
}
26+
}
27+
}
28+
}
29+
},
30+
"tags": [
31+
{
32+
"name": "Project",
33+
"x-amazon-apigateway-tag-value": "MyProject"
34+
},
35+
{
36+
"name": "httpapi:createdBy",
37+
"x-amazon-apigateway-tag-value": "SAM"
38+
}
39+
]
40+
}
41+
},
42+
"Type": "AWS::ApiGatewayV2::Api"
43+
},
44+
"MyHttpApiApiGatewayDefaultStage": {
45+
"Properties": {
46+
"ApiId": {
47+
"Ref": "MyHttpApi"
48+
},
49+
"AutoDeploy": true,
50+
"StageName": "$default",
51+
"Tags": {
52+
"Project": "MyProject",
53+
"httpapi:createdBy": "SAM"
54+
}
55+
},
56+
"Type": "AWS::ApiGatewayV2::Stage"
57+
},
58+
"MyLambdaFunction": {
59+
"Properties": {
60+
"Code": {
61+
"S3Bucket": "my-bucket",
62+
"S3Key": "my-code.zip"
63+
},
64+
"Handler": "hello.handler",
65+
"Role": {
66+
"Fn::GetAtt": [
67+
"MyLambdaFunctionRole",
68+
"Arn"
69+
]
70+
},
71+
"Runtime": "python3.10",
72+
"Tags": [
73+
{
74+
"Key": "lambda:createdBy",
75+
"Value": "SAM"
76+
}
77+
]
78+
},
79+
"Type": "AWS::Lambda::Function"
80+
},
81+
"MyLambdaFunctionRole": {
82+
"Properties": {
83+
"AssumeRolePolicyDocument": {
84+
"Statement": [
85+
{
86+
"Action": [
87+
"sts:AssumeRole"
88+
],
89+
"Effect": "Allow",
90+
"Principal": {
91+
"Service": [
92+
"lambda.amazonaws.com"
93+
]
94+
}
95+
}
96+
],
97+
"Version": "2012-10-17"
98+
},
99+
"ManagedPolicyArns": [
100+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
101+
],
102+
"Tags": [
103+
{
104+
"Key": "lambda:createdBy",
105+
"Value": "SAM"
106+
}
107+
]
108+
},
109+
"Type": "AWS::IAM::Role"
110+
}
111+
}
112+
}

tests/translator/output/aws-cn/httpapi_with_propagate_tags.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@
4848
]
4949
}
5050
},
51-
"FailOnWarnings": true,
52-
"Tags": {
53-
"TagKey1": "Value1",
54-
"TagKey2": "Value2",
55-
"httpapi:createdBy": "SAM"
56-
}
51+
"FailOnWarnings": true
5752
},
5853
"Type": "AWS::ApiGatewayV2::Api"
5954
},

tests/translator/output/aws-us-gov/function_with_events_and_propagate_tags.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,6 @@
564564
"x-amazon-apigateway-tag-value": "SAM"
565565
}
566566
]
567-
},
568-
"Tags": {
569-
"Key1": "Value1",
570-
"Key2": "Value2",
571-
"httpapi:createdBy": "SAM"
572567
}
573568
},
574569
"Type": "AWS::ApiGatewayV2::Api"
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"AWSTemplateFormatVersion": "2010-09-09",
3+
"Resources": {
4+
"MyHttpApi": {
5+
"Properties": {
6+
"Body": {
7+
"info": {
8+
"title": "My API",
9+
"version": "1.0.0"
10+
},
11+
"openapi": "3.0.1",
12+
"paths": {
13+
"/": {
14+
"get": {
15+
"responses": {
16+
"200": {
17+
"description": "OK"
18+
}
19+
},
20+
"x-amazon-apigateway-integration": {
21+
"httpMethod": "POST",
22+
"type": "aws_proxy",
23+
"uri": {
24+
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyLambdaFunction.Arn}/invocations"
25+
}
26+
}
27+
}
28+
}
29+
},
30+
"tags": [
31+
{
32+
"name": "Project",
33+
"x-amazon-apigateway-tag-value": "MyProject"
34+
},
35+
{
36+
"name": "httpapi:createdBy",
37+
"x-amazon-apigateway-tag-value": "SAM"
38+
}
39+
]
40+
}
41+
},
42+
"Type": "AWS::ApiGatewayV2::Api"
43+
},
44+
"MyHttpApiApiGatewayDefaultStage": {
45+
"Properties": {
46+
"ApiId": {
47+
"Ref": "MyHttpApi"
48+
},
49+
"AutoDeploy": true,
50+
"StageName": "$default",
51+
"Tags": {
52+
"Project": "MyProject",
53+
"httpapi:createdBy": "SAM"
54+
}
55+
},
56+
"Type": "AWS::ApiGatewayV2::Stage"
57+
},
58+
"MyLambdaFunction": {
59+
"Properties": {
60+
"Code": {
61+
"S3Bucket": "my-bucket",
62+
"S3Key": "my-code.zip"
63+
},
64+
"Handler": "hello.handler",
65+
"Role": {
66+
"Fn::GetAtt": [
67+
"MyLambdaFunctionRole",
68+
"Arn"
69+
]
70+
},
71+
"Runtime": "python3.10",
72+
"Tags": [
73+
{
74+
"Key": "lambda:createdBy",
75+
"Value": "SAM"
76+
}
77+
]
78+
},
79+
"Type": "AWS::Lambda::Function"
80+
},
81+
"MyLambdaFunctionRole": {
82+
"Properties": {
83+
"AssumeRolePolicyDocument": {
84+
"Statement": [
85+
{
86+
"Action": [
87+
"sts:AssumeRole"
88+
],
89+
"Effect": "Allow",
90+
"Principal": {
91+
"Service": [
92+
"lambda.amazonaws.com"
93+
]
94+
}
95+
}
96+
],
97+
"Version": "2012-10-17"
98+
},
99+
"ManagedPolicyArns": [
100+
"arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
101+
],
102+
"Tags": [
103+
{
104+
"Key": "lambda:createdBy",
105+
"Value": "SAM"
106+
}
107+
]
108+
},
109+
"Type": "AWS::IAM::Role"
110+
}
111+
}
112+
}

tests/translator/output/aws-us-gov/httpapi_with_propagate_tags.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@
4848
]
4949
}
5050
},
51-
"FailOnWarnings": true,
52-
"Tags": {
53-
"TagKey1": "Value1",
54-
"TagKey2": "Value2",
55-
"httpapi:createdBy": "SAM"
56-
}
51+
"FailOnWarnings": true
5752
},
5853
"Type": "AWS::ApiGatewayV2::Api"
5954
},

tests/translator/output/function_with_events_and_propagate_tags.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,6 @@
564564
"x-amazon-apigateway-tag-value": "SAM"
565565
}
566566
]
567-
},
568-
"Tags": {
569-
"Key1": "Value1",
570-
"Key2": "Value2",
571-
"httpapi:createdBy": "SAM"
572567
}
573568
},
574569
"Type": "AWS::ApiGatewayV2::Api"

0 commit comments

Comments
 (0)