Skip to content

Commit 330d14d

Browse files
committed
Allow up to 5 tags to be defined on the EC2 instance role
1 parent 06a2cb1 commit 330d14d

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

templates/aws-stack.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Metadata:
100100
- ManagedPolicyARNs
101101
- InstanceRoleName
102102
- InstanceRolePermissionsBoundaryARN
103+
- InstanceRoleTags
103104
- IMDSv2Tokens
104105
- EnableDetailedMonitoring
105106
- InstanceName
@@ -634,6 +635,12 @@ Parameters:
634635
Description: The ARN of the policy used to set the permissions boundary for the role.
635636
Default: ""
636637

638+
InstanceRoleTags:
639+
Description: "Optional - Comma-separated key=value pairs for instance IAM role tags (up to 5 tags). Example: 'Environment=production,Team=platform,Purpose=ci'. Note: Keys and values cannot contain '=' characters."
640+
Type: String
641+
Default: ""
642+
AllowedPattern: "^$|^[\\w\\s_.:/+\\-@]+=[\\w\\s_.:/+\\-@]*(,[\\w\\s_.:/+\\-@]+=[\\w\\s_.:/+\\-@]*){0,4}$"
643+
637644
InstanceOperatingSystem:
638645
Type: String
639646
Description: The operating system to run on the instances
@@ -894,6 +901,17 @@ Conditions:
894901
SetInstanceRolePermissionsBoundaryARN:
895902
!Not [ !Equals [ !Ref InstanceRolePermissionsBoundaryARN, "" ] ]
896903

904+
UseInstanceRoleTag1:
905+
!Not [ !Equals [ !Select [ "0", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ], "" ] ]
906+
UseInstanceRoleTag2:
907+
!Not [ !Equals [ !Select [ "1", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ], "" ] ]
908+
UseInstanceRoleTag3:
909+
!Not [ !Equals [ !Select [ "2", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ], "" ] ]
910+
UseInstanceRoleTag4:
911+
!Not [ !Equals [ !Select [ "3", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ], "" ] ]
912+
UseInstanceRoleTag5:
913+
!Not [ !Equals [ !Select [ "4", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ], "" ] ]
914+
897915
UseSpecifiedSecretsBucket:
898916
!Not [ !Equals [ !Ref SecretsBucket, "" ] ]
899917

@@ -1317,6 +1335,34 @@ Resources:
13171335
Service: [ autoscaling.amazonaws.com, ec2.amazonaws.com ]
13181336
Action: sts:AssumeRole
13191337
Path: /
1338+
Tags:
1339+
- Key: CreatedBy
1340+
Value: buildkite-elastic-ci-stack
1341+
- !If
1342+
- UseInstanceRoleTag1
1343+
- Key: !Select [ "0", !Split [ "=", !Select [ "0", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
1344+
Value: !Select [ "1", !Split [ "=", !Select [ "0", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
1345+
- !Ref 'AWS::NoValue'
1346+
- !If
1347+
- UseInstanceRoleTag2
1348+
- Key: !Select [ "0", !Split [ "=", !Select [ "1", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
1349+
Value: !Select [ "1", !Split [ "=", !Select [ "1", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
1350+
- !Ref 'AWS::NoValue'
1351+
- !If
1352+
- UseInstanceRoleTag3
1353+
- Key: !Select [ "0", !Split [ "=", !Select [ "2", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
1354+
Value: !Select [ "1", !Split [ "=", !Select [ "2", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
1355+
- !Ref 'AWS::NoValue'
1356+
- !If
1357+
- UseInstanceRoleTag4
1358+
- Key: !Select [ "0", !Split [ "=", !Select [ "3", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
1359+
Value: !Select [ "1", !Split [ "=", !Select [ "3", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
1360+
- !Ref 'AWS::NoValue'
1361+
- !If
1362+
- UseInstanceRoleTag5
1363+
- Key: !Select [ "0", !Split [ "=", !Select [ "4", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
1364+
Value: !Select [ "1", !Split [ "=", !Select [ "4", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
1365+
- !Ref 'AWS::NoValue'
13201366

13211367
IAMPolicies:
13221368
Type: AWS::IAM::Policy

0 commit comments

Comments
 (0)