Open tieto-hemmopaa opened 1 year ago
Hi @tieto-hemmopaa 👋
Just to confirm that there's not a problem in setting the environment variable: Could you try to run echo $TF_VAR_awsAccessKey
to confirm that the value of the variable does not contain quotes ('
) or similar?
I just checked the related Terraform docs, and it appears that you can't use Terraform Variables to configure backends.
We should catch such errors in the CDKTF core library (e.g. using an Aspect, as we already do for other checks) and print a helpful warning.
I'm wondering why Terraform isn't giving a better error, though 🤔
Hi @ansgarm ! No quotes. I've tried setting the environment variables both with and without quotes. Neither of those add them to the value of the variable.
From the docs of the S3 backend:
This can also be sourced from the AWS_ACCESS_KEY_ID environment variable
Sounds like you should be able to pass the required keys using the environment variables defined in the docs of the S3 backend.
Thank you so much @ansgarm !
In case anyone else is wondering, I was able to get my code working by first modifying the code in the following way:
new S3Backend(this, {
bucket: `cdktf-remote-backend-${environment}`,
key: `integration_portal/terraform_${environment}.tfstate`,
encrypt: true,
region: 'eu-west-1',
dynamodbTable: `cdktf-remote-backend-lock-${environment}`,
accessKey: process.env.TF_VAR_awsAccessKey,
secretKey: process.env.TF_VAR_awsSecretAccessKey
});
And running the terraform init -migrate-state
command in cdktf.out/stacks/test-stack-1
.
After that cdktf deploy test-stack-1
works perfectly.
Glad you figured it out!
The only warning I have about using process.env.TF_VAR_awsAccessKey
is that it will cause the secret to end up in the generated cdk.tf.json
file. So be cautious if you commit that file to CI or do something else with it!
I only use process.env.TF_VAR_awsAccessKey
and process.env.TF_VAR_awsSecretAccessKey
when configuring the S3Backend
. 👍 For all the other classes I use the TerraformVariable
.
The secrets used configuring the backend do not seem to end up in the cdk.tf.json
files.
The content of the file for the Backend looks like this:
"backend": {
"s3": {
"bucket": "cdktf-remote-backend-qa",
"dynamodb_table": "cdktf-remote-backend-lock-qa",
"encrypt": true,
"key": "integration_portal/terraform_qa.tfstate",
"region": "eu-west-1"
}
}
Hm, maybe you ran cdktf diff/plan/apply/deploy/destroy
without those environment variables at a later stage? If you have them set, they should end up in the generated file. But if you run synth
(happens automatically for the commands I mentioned previously) it'll recreate that generated file – thereby possibly removing those secrets again.
It does not need to be a bad thing, I just wanted to point that out as it can bring trouble depending on when you set those secrets and what you do with the generated file afterwards.
Sure, I ran all sorts of commands during the process. I'll keep an eye out later on 👍
This just came up in a question on the CDK Dev Slack where CDKTF allowed the value of a AWS SSM Parameter data source to be passed to a Terraform Backend although Terraform did not support this. This seemed to have resulted in an unresolved token that was put into the backend config.
Community Note
cdktf & Language Versions
Node v18.13.0 Terraform v1.3.4 cdktf 0.15.1
Affected Resource(s)
TerraformVariable
Debug Output
Expected Behavior
Creating a TerraformVariable should get the values of environment variables
TF_VAR_awsAccessKey
andTF_VAR_awsSecretAccessKey
.Running
cdktf deploy test-stack-1
orcdktf deploy test-stack-2
should be successful.main.ts:
Environment variables:
Actual Behavior
cdktf deploy test-stack-1
prints the following output:Steps to Reproduce
Create an AWS IAM User with the required permissions.
Make an empty directory and go there
Init the project:
When done, modify the cdktf.json file by adding the provider:
Create the appropriate TypeScript classes for the provider (creates content to the .gen-folder):
Copy the content of my main.ts file to your project.
Add
TF_VAR_awsAccessKey
andTF_VAR_awsSecretAccessKey
to environment variables.Deploy the
test-stack-1
(ortest-stack-2
):Important Factoids
The IAM role used for this deployment does have the correct permissions. The code works fine by hard-coding the environment variable values to the code and getting rid of TerraformVariable.
References
0000