Skip to content
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

Add option to s3 backend to force using the instance profile credentials. #1572

Open
tmccombs opened this issue Apr 29, 2024 · 5 comments
Open
Labels
enhancement New feature or request pending-decision This issue has not been accepted for implementation nor rejected. It's still open to discussion.

Comments

@tmccombs
Copy link

OpenTofu Version

1.6.2

Use Cases

My CI system sets the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

This is great for getting credentials to the account that opentofu is managing resources for.

However, I want to use a single s3 bucket to store the state for stacks across multiple accounts, and reference the outputs with terraform_remote_state data source.

It would be most convenient if I could just use the EC2 instance credentils of the host to access the bucket. However, since AWS credential

It would be more convenient to get this working if I could configure the backend or remote state data source to use the credentials of the ec2 instance. But as far as I can tell there isn't a straighforward way to do that.

Attempted Solutions

There are a couple workarounds for this.

You can grant all of the roles that terraform runs with access to the s3 bucket (and possibly kms key) using cross account permissions on the resource policies. However, this adds a lot of complexity, and doesn't work for granting access to a dynamodb table if locking is used.

You can grant all of the roles permission to assume a role back in the original account, and give that role the permissions needed. However, this is even more complicated than the first solution.

You can have something get temporary credentials from the instance metadata endpoint, and then store those credentials in a file that you reference in the backend or terraform_remote_state data source. But again, this is very complicated.

Proposal

Add an option to the s3 backend, and associated terraform_remote_state configuration that if set to true tells terraform to ignore the environment variables for aws credentials, and instead prefer the instance metadata credentials.

References

No response

@tmccombs tmccombs added enhancement New feature or request pending-decision This issue has not been accepted for implementation nor rejected. It's still open to discussion. labels Apr 29, 2024
@Evi1Pumpkin
Copy link
Collaborator

Hey @tmccombs, thank you for submitting this issue.
I'm not sure I completely understand your use case. Can't you unset the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY env variables, and then use EC2 instance profiles instead (as explained here)?

@tmccombs
Copy link
Author

No, because I need the AWS_ACCESS_KEY_ID and AWS_SECEET_ACCESS_KEY for the aws provider, but I want to use the ec2 instance credentials for the s3 backend.

@sftim
Copy link
Contributor

sftim commented Jun 6, 2024

There are workarounds for this that don't need any change to core Tofu. For example, use profiles with credential_process defined, and write either one or two short wrapper programs to yield the appropriate credentials.

@tmccombs
Copy link
Author

tmccombs commented Jun 6, 2024

I guess that might work.

Although, it is rather more complicated in a CI/CD environment, since instead of just setting environment variables, you have to inject files. One of which needs to be in the runners home directory, which may not be writable.

@lorengordon
Copy link

You could put the aws config file anywhere you like, using the env AWS_CONFIG_FILE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pending-decision This issue has not been accepted for implementation nor rejected. It's still open to discussion.
Projects
None yet
Development

No branches or pull requests

4 participants