Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from samtranslator.model.sns import SNSTopic
from samtranslator.model.stepfunctions import StateMachineGenerator
from samtranslator.model.role_utils import construct_role_for_resource
from samtranslator.model.xray_utils import get_xray_managed_policy_name


class SamFunction(SamResourceMacro):
Expand Down Expand Up @@ -453,13 +454,7 @@ def _construct_role(self, managed_policy_map, event_invoke_policies):

managed_policy_arns = [ArnGenerator.generate_aws_managed_policy_arn("service-role/AWSLambdaBasicExecutionRole")]
if self.Tracing:
# use previous (old) policy name for regular regions
# for china and gov regions, use the newer policy name
partition_name = ArnGenerator.get_partition_name()
if partition_name == "aws":
managed_policy_name = "AWSXrayWriteOnlyAccess"
else:
managed_policy_name = "AWSXRayDaemonWriteAccess"
managed_policy_name = get_xray_managed_policy_name()
managed_policy_arns.append(ArnGenerator.generate_aws_managed_policy_arn(managed_policy_name))
if self.VpcConfig:
managed_policy_arns.append(
Expand Down
7 changes: 6 additions & 1 deletion samtranslator/model/stepfunctions/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from samtranslator.model.tags.resource_tagging import get_tag_list

from samtranslator.model.intrinsics import is_intrinsic
from samtranslator.model.xray_utils import get_xray_managed_policy_name
from samtranslator.utils.cfn_dynamic_references import is_dynamic_reference


Expand Down Expand Up @@ -210,8 +211,12 @@ def _construct_role(self):
:returns: the generated IAM Role
:rtype: model.iam.IAMRole
"""
policies = self.policies[:]
if self.tracing and self.tracing.get("Enabled") is True:
policies.append(get_xray_managed_policy_name())

state_machine_policies = ResourcePolicies(
{"Policies": self.policies},
{"Policies": policies},
# No support for policy templates in the "core"
policy_template_processor=None,
)
Expand Down
10 changes: 10 additions & 0 deletions samtranslator/model/xray_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from samtranslator.translator.arn_generator import ArnGenerator


def get_xray_managed_policy_name():
# use previous (old) policy name for regular regions
# for china and gov regions, use the newer policy name
partition_name = ArnGenerator.get_partition_name()
if partition_name == "aws":
return "AWSXrayWriteOnlyAccess"
return "AWSXRayDaemonWriteAccess"
22 changes: 22 additions & 0 deletions tests/translator/input/state_machine_with_xray_policies.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Resources:
MyFunction:
Type: "AWS::Serverless::Function"
Properties:
CodeUri: s3://sam-demo-bucket/hello.zip
Handler: hello.handler
Runtime: python2.7

StateMachine:
Type: AWS::Serverless::StateMachine
Properties:
Name: MyBasicStateMachine
Type: STANDARD
DefinitionUri: s3://sam-demo-bucket/my-state-machine.asl.json
Tracing:
Enabled: true
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action: lambda:InvokeFunction
Resource: !GetAtt MyFunction.Arn
133 changes: 133 additions & 0 deletions tests/translator/output/aws-cn/state_machine_with_xray_policies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"Resources": {
"MyFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "hello.zip"
},
"Handler": "hello.handler",
"Role": {
"Fn::GetAtt": [
"MyFunctionRole",
"Arn"
]
},
"Runtime": "python2.7",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"MyFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"StateMachine": {
"Type": "AWS::StepFunctions::StateMachine",
"Properties": {
"DefinitionS3Location": {
"Bucket": "sam-demo-bucket",
"Key": "my-state-machine.asl.json"
},
"RoleArn": {
"Fn::GetAtt": [
"StateMachineRole",
"Arn"
]
},
"StateMachineName": "MyBasicStateMachine",
"StateMachineType": "STANDARD",
"Tags": [
{
"Key": "stateMachine:createdBy",
"Value": "SAM"
}
],
"TracingConfiguration": {
"Enabled": true
}
}
},
"StateMachineRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"states.amazonaws.com"
]
}
}
]
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/AWSXRayDaemonWriteAccess"
],
"Policies": [
{
"PolicyName": "StateMachineRolePolicy0",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "lambda:InvokeFunction",
"Resource": {
"Fn::GetAtt": [
"MyFunction",
"Arn"
]
}
}
]
}
}
],
"Tags": [
{
"Key": "stateMachine:createdBy",
"Value": "SAM"
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"Resources": {
"MyFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "hello.zip"
},
"Handler": "hello.handler",
"Role": {
"Fn::GetAtt": [
"MyFunctionRole",
"Arn"
]
},
"Runtime": "python2.7",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"MyFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
},
"ManagedPolicyArns": [
"arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"StateMachine": {
"Type": "AWS::StepFunctions::StateMachine",
"Properties": {
"DefinitionS3Location": {
"Bucket": "sam-demo-bucket",
"Key": "my-state-machine.asl.json"
},
"RoleArn": {
"Fn::GetAtt": [
"StateMachineRole",
"Arn"
]
},
"StateMachineName": "MyBasicStateMachine",
"StateMachineType": "STANDARD",
"Tags": [
{
"Key": "stateMachine:createdBy",
"Value": "SAM"
}
],
"TracingConfiguration": {
"Enabled": true
}
}
},
"StateMachineRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"states.amazonaws.com"
]
}
}
]
},
"ManagedPolicyArns": [
"arn:aws-us-gov:iam::aws:policy/AWSXRayDaemonWriteAccess"
],
"Policies": [
{
"PolicyName": "StateMachineRolePolicy0",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "lambda:InvokeFunction",
"Resource": {
"Fn::GetAtt": [
"MyFunction",
"Arn"
]
}
}
]
}
}
],
"Tags": [
{
"Key": "stateMachine:createdBy",
"Value": "SAM"
}
]
}
}
}
}
Loading