Skip to content

Commit de140b0

Browse files
committed
(fix) pass the condition attribute to the generated function url resources for the conditional functions
1 parent 9306a03 commit de140b0

6 files changed

+379
-2
lines changed

samtranslator/model/sam_resources.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,8 @@ def _construct_function_url(self, lambda_function, lambda_alias):
879879
self._validate_function_url_params(lambda_function)
880880

881881
logical_id = f"{lambda_function.logical_id}Url"
882-
lambda_url = LambdaUrl(logical_id=logical_id)
882+
lambda_url_attributes = self.get_passthrough_resource_attributes()
883+
lambda_url = LambdaUrl(logical_id=logical_id, attributes=lambda_url_attributes)
883884

884885
cors = self.FunctionUrlConfig.get("Cors")
885886
if cors:
@@ -963,7 +964,8 @@ def _construct_url_permission(self, lambda_function):
963964
return None
964965

965966
logical_id = f"{lambda_function.logical_id}UrlPublicPermissions"
966-
lambda_permission = LambdaPermission(logical_id=logical_id)
967+
lambda_permission_attributes = self.get_passthrough_resource_attributes()
968+
lambda_permission = LambdaPermission(logical_id=logical_id, attributes=lambda_permission_attributes)
967969
lambda_permission.Action = "lambda:InvokeFunctionUrl"
968970
lambda_permission.FunctionName = lambda_function.get_runtime_attr("name")
969971
lambda_permission.Principal = "*"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Conditions:
3+
MyCondition:
4+
Fn::Equals:
5+
- true
6+
- true
7+
Parameters: {}
8+
Resources:
9+
MyFunction:
10+
Condition: MyCondition
11+
Type: AWS::Serverless::Function
12+
Properties:
13+
CodeUri: s3://sam-demo-bucket/hello.zip
14+
Description: Created by SAM
15+
Handler: index.handler
16+
MemorySize: 1024
17+
Runtime: nodejs12.x
18+
Timeout: 3
19+
FunctionUrlConfig:
20+
AuthType: NONE
21+
Cors:
22+
AllowOrigins:
23+
- "https://example.com"
24+
- "example1.com"
25+
- "example2.com"
26+
- "example2.com"
27+
AllowMethods:
28+
- "GET"
29+
AllowCredentials: true
30+
AllowHeaders:
31+
- "x-Custom-Header"
32+
ExposeHeaders:
33+
- "x-amzn-header"
34+
MaxAge: 10
35+
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"AWSTemplateFormatVersion": "2010-09-09",
3+
"Conditions": {
4+
"MyCondition": {
5+
"Fn::Equals": [
6+
true,
7+
true
8+
]
9+
}
10+
},
11+
"Parameters": {},
12+
"Resources": {
13+
"MyFunction": {
14+
"Type": "AWS::Lambda::Function",
15+
"Condition": "MyCondition",
16+
"Properties": {
17+
"Code": {
18+
"S3Bucket": "sam-demo-bucket",
19+
"S3Key": "hello.zip"
20+
},
21+
"Description": "Created by SAM",
22+
"Handler": "index.handler",
23+
"MemorySize": 1024,
24+
"Role": {
25+
"Fn::GetAtt": [
26+
"MyFunctionRole",
27+
"Arn"
28+
]
29+
},
30+
"Runtime": "nodejs12.x",
31+
"Timeout": 3,
32+
"Tags": [
33+
{
34+
"Key": "lambda:createdBy",
35+
"Value": "SAM"
36+
}
37+
]
38+
}
39+
},
40+
"MyFunctionUrl": {
41+
"Type": "AWS::Lambda::Url",
42+
"Condition": "MyCondition",
43+
"Properties": {
44+
"TargetFunctionArn": {
45+
"Ref": "MyFunction"
46+
},
47+
"AuthType": "NONE",
48+
"Cors": {
49+
"AllowOrigins": [
50+
"https://example.com",
51+
"example1.com",
52+
"example2.com",
53+
"example2.com"
54+
],
55+
"AllowMethods": [
56+
"GET"
57+
],
58+
"AllowCredentials": true,
59+
"AllowHeaders": [
60+
"x-Custom-Header"
61+
],
62+
"ExposeHeaders": [
63+
"x-amzn-header"
64+
],
65+
"MaxAge": 10
66+
}
67+
}
68+
},
69+
"MyFunctionUrlPublicPermissions": {
70+
"Type": "AWS::Lambda::Permission",
71+
"Condition": "MyCondition",
72+
"Properties": {
73+
"Action": "lambda:InvokeFunctionUrl",
74+
"FunctionName": {
75+
"Ref": "MyFunction"
76+
},
77+
"Principal": "*",
78+
"FunctionUrlAuthType": "NONE"
79+
}
80+
},
81+
"MyFunctionRole": {
82+
"Type": "AWS::IAM::Role",
83+
"Condition": "MyCondition",
84+
"Properties": {
85+
"AssumeRolePolicyDocument": {
86+
"Version": "2012-10-17",
87+
"Statement": [
88+
{
89+
"Action": [
90+
"sts:AssumeRole"
91+
],
92+
"Effect": "Allow",
93+
"Principal": {
94+
"Service": [
95+
"lambda.amazonaws.com"
96+
]
97+
}
98+
}
99+
]
100+
},
101+
"ManagedPolicyArns": [
102+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
103+
],
104+
"Tags": [
105+
{
106+
"Key": "lambda:createdBy",
107+
"Value": "SAM"
108+
}
109+
]
110+
}
111+
}
112+
}
113+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"AWSTemplateFormatVersion": "2010-09-09",
3+
"Conditions": {
4+
"MyCondition": {
5+
"Fn::Equals": [
6+
true,
7+
true
8+
]
9+
}
10+
},
11+
"Parameters": {},
12+
"Resources": {
13+
"MyFunction": {
14+
"Type": "AWS::Lambda::Function",
15+
"Condition": "MyCondition",
16+
"Properties": {
17+
"Code": {
18+
"S3Bucket": "sam-demo-bucket",
19+
"S3Key": "hello.zip"
20+
},
21+
"Description": "Created by SAM",
22+
"Handler": "index.handler",
23+
"MemorySize": 1024,
24+
"Role": {
25+
"Fn::GetAtt": [
26+
"MyFunctionRole",
27+
"Arn"
28+
]
29+
},
30+
"Runtime": "nodejs12.x",
31+
"Timeout": 3,
32+
"Tags": [
33+
{
34+
"Key": "lambda:createdBy",
35+
"Value": "SAM"
36+
}
37+
]
38+
}
39+
},
40+
"MyFunctionUrl": {
41+
"Type": "AWS::Lambda::Url",
42+
"Condition": "MyCondition",
43+
"Properties": {
44+
"TargetFunctionArn": {
45+
"Ref": "MyFunction"
46+
},
47+
"AuthType": "NONE",
48+
"Cors": {
49+
"AllowOrigins": [
50+
"https://example.com",
51+
"example1.com",
52+
"example2.com",
53+
"example2.com"
54+
],
55+
"AllowMethods": [
56+
"GET"
57+
],
58+
"AllowCredentials": true,
59+
"AllowHeaders": [
60+
"x-Custom-Header"
61+
],
62+
"ExposeHeaders": [
63+
"x-amzn-header"
64+
],
65+
"MaxAge": 10
66+
}
67+
}
68+
},
69+
"MyFunctionUrlPublicPermissions": {
70+
"Type": "AWS::Lambda::Permission",
71+
"Condition": "MyCondition",
72+
"Properties": {
73+
"Action": "lambda:InvokeFunctionUrl",
74+
"FunctionName": {
75+
"Ref": "MyFunction"
76+
},
77+
"Principal": "*",
78+
"FunctionUrlAuthType": "NONE"
79+
}
80+
},
81+
"MyFunctionRole": {
82+
"Type": "AWS::IAM::Role",
83+
"Condition": "MyCondition",
84+
"Properties": {
85+
"AssumeRolePolicyDocument": {
86+
"Version": "2012-10-17",
87+
"Statement": [
88+
{
89+
"Action": [
90+
"sts:AssumeRole"
91+
],
92+
"Effect": "Allow",
93+
"Principal": {
94+
"Service": [
95+
"lambda.amazonaws.com"
96+
]
97+
}
98+
}
99+
]
100+
},
101+
"ManagedPolicyArns": [
102+
"arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
103+
],
104+
"Tags": [
105+
{
106+
"Key": "lambda:createdBy",
107+
"Value": "SAM"
108+
}
109+
]
110+
}
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)