Amazon Web Services (AWS)ยป
Let's Explainยป
The AWS integration allows either Spacelift runs or tasks to automatically assume an IAM role in your AWS account, and in the process, generate a set of temporary credentials. These credentials are then exposed as computed environment variables during the run/task that takes place on the particular Spacelift stack that the integration is attached to.
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_SECURITY_TOKEN
AWS_SESSION_TOKEN
This is enough for both the AWS Terraform provider and/or Amazon S3 state backend to generate a fully authenticated AWS session without further configuration. However, you will likely need to select one of the available regions with the former.
Usageยป
To utilize the AWS integration, you need to set up at least one cloud integration, and then attach that integration to any stacks that need it. Please follow the Setup Guide for more information on this process.
Trust Policyยป
When setting up a Spacelift AWS Cloud Integration you need to specify the ARN of an IAM Role to use. The Trust Policy for this role must be configured to allow Spacelift to assume the role and generate temporary credentials.
When completing the role assumption, Spacelift will pass extra information in the ExternalId
attribute, allowing you to optionally add additional layers of security to your role.
External ID Format: <spacelift-account-name>@<integration-id>@<stack-slug>@<read|write>
<spacelift-account-name>
: the name of the Spacelift account that initiated the role assumption.<integration-id>
: the ID of the AWS Cloud Integration that initiated the role assumption.<stack-slug>
: the slug of the stack that the AWS Cloud Integration is attached to, that initiated the role assumption.<read|write>
: set to eitherread
orwrite
based upon the event occurring that has initiated the role assumption. The Planning phase utilizesread
while the Applying phase utilizeswrite
.
Setup Guideยป
Prerequisites:
- The ability to create IAM Roles in your AWS account.
- Admin access to your Spacelift account.
Setup a Role in AWSยป
Before creating the Spacelift AWS integration, you need to have an AWS IAM Role within your AWS account that the cloud integration will use.
Within your AWS account, navigate to AWS IAM and click the Create role button.
Hint
To allow this integration to access multiple AWS accounts, you can extend this role to have cross-account permissions to the target accounts. See AWS documentation for more details.
Configure Trust Policyยป
Next, we want to configure the Trust Policy for the role to allow Spacelift to assume the role.
Here's an example trust policy statement you can use, that allows any stack within your Spacelift account to use this IAM Role:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Info
Be sure to replace yourSpaceliftAccountName in the example above with your actual Spacelift account name.
Info
Ensure you replace <principal>
based on your environment.
1 2 |
|
Optionally Configure Further Constraints on the Trust Policyยป
Info
By default, Spacelift passes the ExternalId
value in this format: <spacelift-account-name>@<integration-id>@<stack-slug>@<read|write>
Knowing the format of the External ID passed by Spacelift, you can further secure your IAM Role trust policies if you desire a deeper level of granular security.
For example, you may wish to lock down an IAM Role so that it can only be used by a specific stack. The following example shows how to lock down an IAM Role so that it can only be assumed by the stack stack-a
in a Spacelift account called example
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Info
Ensure you replace <principal>
based on your environment.
- for spacelift.io, use
324880187172
. - for us.spacelift.io, use
577638371743
.
Configure Role Permissionsยป
Next, you need to attach at least one IAM Policy to your IAM Role to provide it with sufficient permissions to deploy any resources that your IaC code defines.
Info
For Terraform users that are managing their own state file, don't forget to give your role sufficient permissions to access your state (Terraform documents the permissions required for S3-managed state here, and for DynamoDB state locking here).
Create IAM Roleยป
Once you have your IAM Role's trust policy and IAM Policies configured, you can finish creating the role. Take a note of the IAM Role ARN, as you'll need this when setting up the integration in Spacelift in the next section.
Navigate to Cloud Integrationsยป
Now that you have an IAM Role created, navigate to the Cloud Integrations page from the Spacelift navigation sidebar.
Create an Integrationยป
Click on the Set up integration button and select AWS in the drop-down menu to start configuring your integration:
When creating an integration, you will immediately notice that you need to specify two required fields: Name and Role ARN. Give the integration a name of your choosing, and paste in the ARN of the IAM Role that you just created.
If you enable the assume role on worker option, the role assumption will be performed on your private worker rather than at Spacelift's end. When role assumption on the worker is enabled, you can also optionally specify a custom External ID to use during role assumption.
Info
When creating your role in AWS, you need to ensure the role has a trust policy that allows Spacelift to assume the role to generate temporary credentials for runs. Assuming you are following this guide, you should have configured this in the previous section. For troubleshooting, refer to the Troubleshooting Trust Relationship Issues section.
Using the Integrationยป
Now that the integration has been created, you need to attach it to one or more stacks. To do this, navigate to a stack that you want to attach your integration to:
Next, go to the stack's settings:
Choose the integrations tab:
Click Attach cloud integration button, then select the AWS option, choose your integration, and select whether it should be used for read, write or both read and write phases:
Read vs Writeยป
You can attach an AWS integration as read, write or read-write, and you can attach at most two integrations to any single stack. Read indicates that this integration will be used during read phases of runs (for example, plans), and Write indicates that this integration will be used during write phases of runs (for example, applies).
Role Assumption Verificationยป
If the Cloud Integration has the "Assume Role on Worker" setting disabled, Spacelift will verify the role assumption as soon as you click the attach button. If role assumption succeeds, it will try to assume the role without the unique external ID, and this time it expects to fail. If Spacelift fails the latter check, we consider the integration is safely configured.
Success
This somewhat counterintuitive extra check is to prevent against malicious takeover of your account by someone who happens to know your AWS account ID, which isn't all that secret, really. The security vulnerability we're addressing here is known as the confused deputy problem.
Troubleshooting Trust Relationship Issuesยป
If you get the error you need to configure trust relationship section in your AWS account
when attaching a cloud integration to a stack:
There are a couple of common causes to check.
Incorrect or Missing Trust Relationship Policyยป
The error message in the UI includes a tailored trust relationship policy example. This policy allows Spacelift to assume the IAM role and must be added to the Trust relationships section of your role in AWS IAM. Check previous section for more information on how to configure the trust policy.
STS (Security Token Service) Not Enabledยป
This error can occur if the AWS STS (Security Token Service) is not enabled in your account.
Make sure STS is enabled in the following regions:
- eu-west-1
- eu-central-1 (used for disaster recovery failover)
You can enable STS by following this AWS guide.
Programmatic Setupยป
You can also use the Spacelift Terraform provider in order to create an AWS Cloud integration from an administrative stack, including the trust relationship. Note that in order to do that, your administrative stack will require AWS credentials itself, and ones powerful enough to be able to deal with IAM.
Here's a little example of what that might look like to create a Cloud Integration programmatically:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
Info
Please always refer to the provider documentation for the most up-to-date documentation.
Attaching a Role to Multiple Stacksยป
The previous example explained how to use the spacelift_aws_integration_attachment_external_id
data-source to generate the assume role policy for using the integration with a single stack, but what if you want to attach the integration to multiple stacks? The simplest option would be to create multiple instances of the data-source - one for each stack - but you can also use a Terraform for_each
condition to reduce the amount of code required:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
This will generate a trust relationship that looks something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Info
The value of <principal>
will be based on your environment.
- for spacelift.io, it will be
324880187172
. - for us.spacelift.io, it will be
577638371743
.
Is it safe?ยป
Assuming roles and generating credentials on the private worker is perfectly safe. Those credentials are never leaked to us in any shape or form. Hence, the rest of this section discusses the trust relationship established between the Spacelift account and your AWS account for the purpose of dynamically generating short-lived credentials. So, how safe is that?
Probably safer than storing static credentials in your stack environment. Unlike user keys that you'd normally have to use, role credentials are dynamically created and short-lived. We use the default expiration which is 1 hour, and do not store them anywhere. Leaking them accidentally through the logs is not an option either because we mask AWS credentials.
The most tangible safety feature of the AWS integration is the breadcrumb trail it leaves in CloudTrail. Every resource change can be mapped to an individual Terraform run or task whose ID automatically becomes the username as the sts:AssumeRole
API call with that ID as RoleSessionName
. In conjunction with AWS tools like Config, it can be a very powerful compliance tool.
Let's have a look at a CloudTrail event showing an IAM role being created by what seems to be a Spacelift run:
01DSJ63P40BAZY4VW8BXXG7M4K
is indeed a run ID we can then trace back even further.
Roles assuming other rolesยป
OK, we get it. Using everyone's favorite Inception meme:
Indeed, the AWS Terraform provider allows you to assume an IAM role during setup, effectively doing the same thing over again. This approach is especially useful if you want to control resources in multiple AWS accounts from a single Spacelift stack. This is totally fine - in IAM, roles can assume other roles, though what you need to do on your end is set up the trust relationship between the role you have Spacelift assume and the role for each provider instance to assume. But let's face it - at this level of sophistication, you sure know what you're doing.
One bit you might not want to miss though, is the guaranteed ability to map the change to a particular run or task that we described in the previous section. One way of fixing that would be to use the TF_VAR_spacelift_run_id
computed environment variable available to each Spacelift workflow. Conveniently, it's already a Terraform variable, so a setup like this should do the trick:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|