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

Bug: AWS SAM CLI with Terraform does not read TF_VAR_** environment values for Windows #5670

Closed
s-stefanov opened this issue Aug 1, 2023 · 6 comments
Assignees
Labels
area/terraform terraform support issue blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. platform/windows

Comments

@s-stefanov
Copy link

s-stefanov commented Aug 1, 2023

Description:

Windows specific
Executing sam build --hook-name terraform --beta-features
Does not reads Terraform Vars using Environment Variables TF_VAR_**

Steps to reproduce:

  1. Create Bat or PowerShell script setting some environment variables to be used by Terraform
    $Env:TF_VAR_aws_account='some_account'
    sam build --hook-name terraform --beta-features

Observed result:

Terraform Plan still asks for input for the variables defined in the TF files

Expected result:

Terraform reads these variables from Environment

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Windows 11
  2. sam --version: SAM CLI, version 1.94.0
  3. AWS region: eu-west-2
{
  "version": "1.94.0",
  "system": {
    "python": "3.8.8",
    "os": "Windows-10-10.0.22621-SP0"
  },
  "additional_dependencies": {
    "docker_engine": "24.0.2",
    "aws_cdk": "Not available",
    "terraform": "1.5.4"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

Add --debug flag to command you are running

@s-stefanov s-stefanov added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Aug 1, 2023
@jfuss jfuss changed the title Bug: TITLE Bug: AWS SAM CLI with Terraform does not read TF_VAR_** environment values for Windows Aug 1, 2023
@s-stefanov
Copy link
Author

s-stefanov commented Aug 1, 2023

Thanks jfuss! As an addition, if I use terraform init and terraform plan directly in the PowerShell, it's working as expected and reads the environment variables.

@moelasmar moelasmar self-assigned this Aug 1, 2023
@moelasmar
Copy link
Contributor

thanks @s-stefanov for raising this issue, I will work on reproducing this issue and see how can we resolve it.

@moelasmar moelasmar added platform/windows area/terraform terraform support issue labels Aug 1, 2023
@s-stefanov
Copy link
Author

@moelasmar I manage to have a workaround with a terraform.tfvars file in the root directory. I tried with several approach to pass the envrionment variables, but each time the Command Line input is asked - With PowerShell $Env and Batch SET.
The environment variables are set up correctly in the Terraform Plan output, but then asked again for entering a value.

@jfuss jfuss removed the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Aug 4, 2023
@moelasmar
Copy link
Contributor

sorry @s-stefanov for late reply, I used this sample TF project to test passing the TF variables as environment variable in windows.

As you see here

variable "HELLO_FUNCTION_SRC_CODE" {
type = string
default = "../src/artifacts/HelloWorldFunction"
}

The project expects a variable to refer to the Lambda function source code, I executed the following commands on Powershell:

> $Env:TF_VAR_HELLO_FUNCTION_SRC_CODE="../src/artifacts/HelloWorldFunction2"

> samdev build --hook-name terraform --beta-features --terraform-project-root-path ./..
2023-08-15 14:14:33 Attaching import module proxy for analyzing dynamic imports

Experimental features are enabled for this session.
Visit the docs page to learn more about the AWS Beta terms https://aws.amazon.com/service-terms/.

Running Prepare Hook to prepare the current application
Executing prepare hook of hook "terraform"
Initializing Terraform application
.........
Creating terraform plan and getting JSON output
...........
Generating metadata file
....
Build Succeeded

Built Artifacts  : .aws-sam\build
Built Template   : .aws-sam\build\template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke --hook-name terraform
[*] Emulate local Lambda functions: sam local start-lambda --hook-name terraform

> samdev local invoke --hook-name terraform --beta-features module.function2.aws_lambda_function.this
2023-08-15 14:19:52 Attaching import module proxy for analyzing dynamic imports

Experimental features are enabled for this session.
Visit the docs page to learn more about the AWS Beta terms https://aws.amazon.com/service-terms/.

Skipped prepare hook. Current application is already prepared.
Invoking app.lambda_handler (python3.8)
ModuleLayer2AwsLambdaLayerVersionLayer60C80792 is a local Layer in the template
Local image is up-to-date
Building image....................
Using local image: samcli/lambda-python:3.8-x86_64-692342758e2cb5e0ffee713e6.

Mounting
C:\mohamed\aws-sam-cli\tests\integration\testdata\buildcmd\terraform\application_outside_root_directory_windows\root_mod
ule\.aws-sam\build\ModuleFunction2AwsLambdaFunctionThis6697C0C3 as /var/task:ro,delegated, inside runtime container
START RequestId: 1d642a18-35a7-4242-936d-c2d440c4097e Version: $LATEST
END RequestId: 1d642a18-35a7-4242-936d-c2d440c4097e
REPORT RequestId: 1d642a18-35a7-4242-936d-c2d440c4097e  Init Duration: 0.19 ms  Duration: 1438.97 ms    Billed Duration: 1439 ms        Memory Size: 128 MB     Max Memory Used: 128 MB
{"statusCode": 200, "body": "hello world from copy 1"}

I also get it working on CMD. Could you please share with us your Terraform Project to investigate more.

@moelasmar moelasmar added blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days labels Aug 15, 2023
@moelasmar
Copy link
Contributor

Thanks for reporting this. Closing this issue due to inactivity, please create new issue if you come across any more issues.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 2, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/terraform terraform support issue blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. platform/windows
Projects
None yet
Development

No branches or pull requests

3 participants