Skip to content

Instantly share code, notes, and snippets.

@Ashex
Created August 20, 2015 15:32
Show Gist options
  • Save Ashex/0e3da12c0798374b593c to your computer and use it in GitHub Desktop.
Save Ashex/0e3da12c0798374b593c to your computer and use it in GitHub Desktop.
Dynamic VPC restriction for IAM policy
policy_condition = Join("", ["arn:aws:ec2:", Ref("AWS::AccountId"), ":vpc/" ,Ref(VPC)])
t.add_resource(
Role(
"natEc2Role",
Path="/",
AssumeRolePolicyDocument=
{ "Statement": [ {
"Effect": "Allow",
"Principal": { "Service": [ "ec2.amazonaws.com" ] },
"Action": [ "sts:AssumeRole" ]
} ] }
,
Policies=[
Policy(
PolicyName="attachNetworkInterface",
PolicyDocument={
"Statement": [ {
"Effect": "Allow",
"Action": "ec2:attachNetworkInterface",
"Resource": "*",
"Condition": {
"StringEquals": {
"ec2:VPC": policy_condition
},
}
} ],
}
)
]
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment