cloudtools / cloudtools/troposphere

Better support for conditionals inside properties

Open
#668 3 comments 4 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
4.9k
Forks
1.4k
PR merge metrics
No merged PRs in 30d

Description

Conditionals like IF can be used inside some properties, but because of strict type checking, troposphere does not allow for their use.

For example, the following is a correct template snippet (not a specific use-case I had, just an example):
```
"InstanceRolePolicy": {
"Properties": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"logs:PutLogEvents"
],
"Effect": "Allow",
"Resource": [
"arn:aws:logs:*:*:*"
]
},
{
"Fn::If": [
"SomeConditionHere",
{
"Action": [
"logs:CreateLogStream"
],
"Effect": "Allow",
"Resource": [
"arn:aws:logs:*:*:*"
]
},
{
"Ref": "AWS::NoValue"
}
]
}
]
},
"PolicyName": "test",
"Roles": [
{
"Ref": "InstanceRole"
}
]
},
"Type": "AWS::IAM::Policy"
},
```

which should have a representation in troposphere as below:
```
iam.PolicyType(
"InstanceRolePolicy",
PolicyName="test",
PolicyDocument=aws.Policy(
Statement=[
aws.Statement(
Effect=aws.Allow,
Action=[
aws.Action("logs", "PutLogEvents"),
],
Resource=["arn:aws:logs:*:*:*"]
),
If(
"SomeConditionHere",
aws.Statement(
Effect=aws.Allow,
Action=[
aws.Action("logs", "CreateLogStream"),
],
Resource=["arn:aws:logs:*:*:*"]
),
Ref(AWS_NO_VALUE)
)
]
),
)
```

but it obviously fails with `TypeError: Statement is , expected []`

This specific example is related to awacs, but I imagine the same problems happen directly in troposphere as well, wherever the property is expected to be a specific object.

Should conditionals be allowed by troposphere inside most (all?) properties? Either via something simple to how `policytypes` are solved in `iam` or by something more complex which will detect the condition and check for the types inside the condition?

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the TypeError with the nested AWS IAM policy example and inspect troposphere's strict property type checking and If handling. Define the supported conditional cases and verify that conditional statements are accepted while the expected property types remain validated.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python
Domain
cloud
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.