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

Terraform looks up wrong AWS Credentials in ECS #2700

Closed
nocode99 opened this issue Dec 18, 2017 · 10 comments
Closed

Terraform looks up wrong AWS Credentials in ECS #2700

nocode99 opened this issue Dec 18, 2017 · 10 comments
Labels
bug Addresses a defect in current functionality. provider Pertains to the provider itself, rather than any interaction with AWS. service/ecs Issues and PRs that pertain to the ecs service.
Milestone

Comments

@nocode99
Copy link

If you look at my issue from Vault, I'm experiencing similar issues with Terraform hashicorp/vault#2979

I'm trying to run terraform on jenkins slave nodes in ECS and I have verified it is trying to get permissions from the host. I use KMS key and kept getting access denied and when I gave my EC2 instance permissions to it as a test, it was able to use the KMS key in the remote state config.

Terraform Version

0.10.5
Providers: 1.4.0, 1.5.0

Affected Resource(s)

Everything :)

Expected Behavior

When using terraform in ECS, it should be looking up the credentials for the ECS Task Role ARN and not the instance profile of the EC2 instance

Actual Behavior

Terraform tries to use the credentials of the EC2 instance profile

Important Factoids

This issue was happening in 0.8.7 and I lazily just set static credentials for the time being and rotated them routinely. Based on the linked Vault issue, this is the way the Go SDK handles environment variables.

References

hashicorp/vault#2979

@myoung34
Copy link
Contributor

@nocode99 related? hashicorp/terraform#8746

@jen20 jen20 added the bug Addresses a defect in current functionality. label Dec 29, 2017
@radeksimko radeksimko added the service/ecs Issues and PRs that pertain to the ecs service. label Jan 28, 2018
@evanstachowiak
Copy link

evanstachowiak commented Feb 13, 2018

This is also an issue for me on 0.10.7 and 0.11.3.

I've resorted to manually setting the credentials this way:
curl --silent http://169.254.169.254:80/latest/meta-data/iam/security-credentials/$role_name > credentials && export AWS_ACCESS_KEY_ID=$(jq '.AccessKeyId' credentials) && export AWS_SECRET_ACCESS_KEY=$(jq '.SecretAccessKey' credentials) && export AWS_SESSION_TOKEN=$(jq '.Token' credentials) && rm credentials

I've just recently upgraded my ECS agent, perhaps this could be a cause? It is only happening now after the upgrade.

@bflad bflad added provider Pertains to the provider itself, rather than any interaction with AWS. and removed service/ecs Issues and PRs that pertain to the ecs service. labels Feb 15, 2018
@PurrBiscuit
Copy link
Contributor

PurrBiscuit commented Mar 14, 2018

I'm seeing the same behaviour with version 1.11.0 of the AWS Terraform provider.

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "vault" (1.0.0)...
- Downloading plugin for provider "aws" (1.11.0)...

We have the task running with a Task ARN but it's still falling back to the EC2 IAM Role that's on the instance in the ECS cluster that the task is being run on.

* aws_iam_role.poco: aws_iam_role.poco: Error reading IAM Role poco-us-east-1: AccessDenied: User: arn:aws:sts::111111111111:assumed-role/jenkins-ecs/i-0809111111111 is not authorized to perform: iam:GetRole on resource: role poco-us-east-1

	status code: 403, request id: b9b7f0dd-27ba-11e8-b4d1-ebaba213a394

We're currently using ECS Agent version 1.17.1 in that ECS cluster as well.

@JohannesEbke
Copy link

JohannesEbke commented Apr 10, 2018

I'm observing a similar thing: When running terraform on an AWS instance, credentials in ~/.aws/config are ignored in favor of the instance profile. This seems to contradict the "credential provider chain" specified in https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html

Unfortunately the workaround of setting the AWS_* environment variables does not work, since we use profiles with assumed roles from ~/.aws/config, and they don't get picked up when the AWS_ credentials are specified.

@paultyng paultyng added the service/ecs Issues and PRs that pertain to the ecs service. label Jun 25, 2018
@paultyng
Copy link
Contributor

Have you tried using the ECS_AWSVPC_BLOCK_IMDS environment variable?

To prevent containers in tasks that use the awsvpc network mode from accessing the credential information supplied to the container instance profile (while still allowing the permissions that are provided by the task role), set the ECS_AWSVPC_BLOCK_IMDS agent configuration variable to true in the agent configuration file and restart the agent.

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html

@paultyng
Copy link
Contributor

It does seem like the provider looks up the value of AWS_CONTAINER_CREDENTIALS_RELATIVE_URI in the code:

https://github.com/terraform-providers/terraform-provider-aws/blob/8427cf84775dae2be92d8da63d037364895c9955/aws/auth_helpers.go#L153-L157

So perhaps its just a precedence problem?

@aeschright
Copy link
Contributor

Hey folks, we're rolling out a fix in the next release that should take care of the credential precedence issues you were seeing. If there's still a problem after that, please open a new bug issue so we can look into the details. Thanks!

@aeschright aeschright added this to the v2.32.0 milestone Oct 8, 2019
@bflad
Copy link
Contributor

bflad commented Oct 10, 2019

Closing as #10379 was merged previously and v2.32.0 has been released. 👍

@bflad bflad closed this as completed Oct 10, 2019
@ghost
Copy link

ghost commented Oct 10, 2019

This has been released in version 2.32.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!

@ghost
Copy link

ghost commented Nov 10, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Nov 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. provider Pertains to the provider itself, rather than any interaction with AWS. service/ecs Issues and PRs that pertain to the ecs service.
Projects
None yet
Development

No branches or pull requests

10 participants