aws / aws-sdk-go-v2

AWS SDK for the Go programming language.
https://aws.github.io/aws-sdk-go-v2/docs/
Apache License 2.0
2.68k stars 651 forks source link

Unable to remove environment variables from `amplify` App #2788

Closed jar-b closed 2 months ago

jar-b commented 2 months ago

Acknowledgements

Describe the bug

After creating an amplify app with environment variables configured, there is no way to remove them. The EnvironmentVariables field is a map[string]string and I've attempted the following.

  1. Set to map[string]string{}
  2. Set to nil
  3. Set to map[string]string{"": ""}

I'd expect either 1 or 2 to from above to remove the existing environment variables. 3 at one point worked with AWS SDK for Go V1 (see the Terraform AWS provider implementation and acceptance test), but even V1 now returns an error.

Regression Issue

Expected Behavior

The ability to remove configured environment variables.

Current Behavior

Not possible to remove environment variables once configured.

Reproduction Steps

  1. Run the program below.
  2. Observe environment variables persisting in response output.
package main

import (
    "context"
    "fmt"
    "log"

    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/amplify"
)

func main() {
    ctx := context.TODO()
    cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion("us-west-2"))
    if err != nil {
        log.Fatalf("unable to load SDK config, %v", err)
    }

    client := amplify.NewFromConfig(cfg)

    fmt.Println("Creating app...")
    out, err := client.CreateApp(ctx, &amplify.CreateAppInput{
        Name: aws.String("jb-test"),
        EnvironmentVariables: map[string]string{
            "foo": "bar",
        },
    })
    if err != nil {
        log.Fatal(err)
    }
    appId := out.App.AppId

    fmt.Println("Attempt 1 -Updating to an empty map...")
    out1, err := client.UpdateApp(ctx, &amplify.UpdateAppInput{
        AppId:                appId,
        EnvironmentVariables: map[string]string{},
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("result: %s\n", out1.App.EnvironmentVariables)

    fmt.Println("Attempt 2 - Updating to nil...")
    out2, err := client.UpdateApp(ctx, &amplify.UpdateAppInput{
        AppId:                appId,
        EnvironmentVariables: nil,
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("result: %s\n", out2.App.EnvironmentVariables)

    fmt.Println("Attempt 3 - Updating to a map with an empty key/value pair...")
    _, err = client.UpdateApp(ctx, &amplify.UpdateAppInput{
        AppId:                appId,
        EnvironmentVariables: map[string]string{"": ""},
    })
    if err != nil {
        // This previously worked with AWS SDK for Go V1, but is now an error
        // Ref: https://github.com/hashicorp/terraform-provider-aws/blob/898c9b5a1d8958366b293dad02daa44e24e360ef/internal/service/amplify/app.go#L510-L516
        fmt.Println(err)
    }

    fmt.Println("Deleting app...")
    _, err = client.DeleteApp(ctx, &amplify.DeleteAppInput{
        AppId: appId,
    })
    if err != nil {
        log.Fatal(err)
    }
}

Result:

Creating app...
Attempt 1 -Updating to an empty map...
result: map[foo:bar]
Attempt 2 - Updating to nil...
result: map[foo:bar]
Attempt 3 - Updating to a map with an empty key/value pair...
operation error Amplify: UpdateApp, https response error StatusCode: 400, RequestID: 590409c6-6695-435b-ab03-b579db62576f, BadRequestException: Environment variables cannot have an empty key.
Deleting app...

Possible Solution

No response

Additional Information/Context

No response

AWS Go SDK V2 Module Versions Used

module main

go 1.23.1

require (
    github.com/aws/aws-sdk-go-v2 v1.30.5
    github.com/aws/aws-sdk-go-v2/config v1.27.33
    github.com/aws/aws-sdk-go-v2/service/amplify v1.24.3
)

require (
    github.com/aws/aws-sdk-go-v2/credentials v1.17.32 // indirect
    github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.13 // indirect
    github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.17 // indirect
    github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.17 // indirect
    github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
    github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 // indirect
    github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.19 // indirect
    github.com/aws/aws-sdk-go-v2/service/sso v1.22.7 // indirect
    github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.7 // indirect
    github.com/aws/aws-sdk-go-v2/service/sts v1.30.7 // indirect
    github.com/aws/smithy-go v1.20.4 // indirect
)

Compiler and Version used

go version go1.23.1 darwin/arm64

Operating System and version

MacOS Sonoma 14.6.1

bhavya2109sharma commented 2 months ago

Hello @jar-b,

Thanks for reaching out.

To delete the environment variables, you need to set the key to a space character and the value to an empty string. Here is the code that will delete the environment variables:

fmt.Println("Deleting environment variables...")
    out1, err := client.UpdateApp(ctx, &amplify.UpdateAppInput{
        AppId: aws.String(appId),
        EnvironmentVariables: map[string]string{
            " ": "",
        },
    })

Thanks ~Bhavya

github-actions[bot] commented 2 months ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.

jar-b commented 2 months ago

Thank you for the clarification @bhavya2109sharma!