Skip to content

Allow up to 5 tags to be defined on the EC2 instance role #1556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions templates/aws-stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Metadata:
- ManagedPolicyARNs
- InstanceRoleName
- InstanceRolePermissionsBoundaryARN
- InstanceRoleTags
- IMDSv2Tokens
- EnableDetailedMonitoring
- InstanceName
Expand Down Expand Up @@ -634,6 +635,12 @@ Parameters:
Description: The ARN of the policy used to set the permissions boundary for the role.
Default: ""

InstanceRoleTags:
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."
Type: String
Default: ""
AllowedPattern: "^$|^[\\w\\s_.:/+\\-@]+=[\\w\\s_.:/+\\-@]*(,[\\w\\s_.:/+\\-@]+=[\\w\\s_.:/+\\-@]*){0,4}$"

InstanceOperatingSystem:
Type: String
Description: The operating system to run on the instances
Expand Down Expand Up @@ -894,6 +901,17 @@ Conditions:
SetInstanceRolePermissionsBoundaryARN:
!Not [ !Equals [ !Ref InstanceRolePermissionsBoundaryARN, "" ] ]

UseInstanceRoleTag1:
!Not [ !Equals [ !Select [ "0", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ], "" ] ]
UseInstanceRoleTag2:
!Not [ !Equals [ !Select [ "1", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ], "" ] ]
UseInstanceRoleTag3:
!Not [ !Equals [ !Select [ "2", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ], "" ] ]
UseInstanceRoleTag4:
!Not [ !Equals [ !Select [ "3", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ], "" ] ]
UseInstanceRoleTag5:
!Not [ !Equals [ !Select [ "4", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ], "" ] ]

UseSpecifiedSecretsBucket:
!Not [ !Equals [ !Ref SecretsBucket, "" ] ]

Expand Down Expand Up @@ -1317,6 +1335,34 @@ Resources:
Service: [ autoscaling.amazonaws.com, ec2.amazonaws.com ]
Action: sts:AssumeRole
Path: /
Tags:
- Key: CreatedBy
Value: buildkite-elastic-ci-stack
- !If
- UseInstanceRoleTag1
- Key: !Select [ "0", !Split [ "=", !Select [ "0", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
Value: !Select [ "1", !Split [ "=", !Select [ "0", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
- !Ref 'AWS::NoValue'
- !If
- UseInstanceRoleTag2
- Key: !Select [ "0", !Split [ "=", !Select [ "1", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
Value: !Select [ "1", !Split [ "=", !Select [ "1", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
- !Ref 'AWS::NoValue'
- !If
- UseInstanceRoleTag3
- Key: !Select [ "0", !Split [ "=", !Select [ "2", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
Value: !Select [ "1", !Split [ "=", !Select [ "2", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
- !Ref 'AWS::NoValue'
- !If
- UseInstanceRoleTag4
- Key: !Select [ "0", !Split [ "=", !Select [ "3", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
Value: !Select [ "1", !Split [ "=", !Select [ "3", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
- !Ref 'AWS::NoValue'
- !If
- UseInstanceRoleTag5
- Key: !Select [ "0", !Split [ "=", !Select [ "4", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
Value: !Select [ "1", !Split [ "=", !Select [ "4", !Split [ ",", !Join [ ",", [ !Ref InstanceRoleTags, "", "", "", "", "" ] ] ] ] ] ]
- !Ref 'AWS::NoValue'

IAMPolicies:
Type: AWS::IAM::Policy
Expand Down