Skip to content

Commit

Permalink
Replace resources when the AWS region changes (#4000)
Browse files Browse the repository at this point in the history
Enable `pulumi up` to do the right thing when AWS region changes in the
program. It will now execute a cascading replace plan for all resources
associated with a provider to delete resources in the old region and
re-provision them in the new desired region.

Fixes #3777
#879
  • Loading branch information
t0yv0 authored May 31, 2024
1 parent ae08c14 commit bbb6123
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 0 deletions.
30 changes: 30 additions & 0 deletions provider/provider_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,36 @@ func TestRegress3835(t *testing.T) {
t.Logf("#%v", result.ChangeSummary)
}

func TestChangingRegion(t *testing.T) {
skipIfShort(t)
dir := filepath.Join("test-programs", "changing-region")
cwd, err := os.Getwd()
require.NoError(t, err)
providerName := "aws"
options := []opttest.Option{
opttest.LocalProviderPath(providerName, filepath.Join(cwd, "..", "bin")),
opttest.YarnLink("@pulumi/aws"),
}

t.Run("default provider", func(t *testing.T) {
test := pulumitest.NewPulumiTest(t, dir, options...)
for _, region := range []string{"us-east-1", "us-west-1"} {
test.SetConfig("aws:region", region)
res := test.Up()
require.Equal(t, region, res.Outputs["actualRegion"].Value)
}
})

t.Run("explicit provider", func(t *testing.T) {
test := pulumitest.NewPulumiTest(t, dir, options...)
for _, region := range []string{"us-east-1", "us-west-1"} {
test.SetConfig("desired-region", region)
res := test.Up()
require.Equal(t, region, res.Outputs["actualRegion"].Value)
}
})
}

func TestRegressAttributeMustBeWholeNumber(t *testing.T) {
// pulumi/pulumi-terraform-bridge#1940
skipIfShort(t)
Expand Down
1 change: 1 addition & 0 deletions provider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ func ProviderFromMeta(metaInfo *tfbridge.MetadataInfo) *tfbridge.ProviderInfo {
Default: &tfbridge.DefaultInfo{
EnvVars: []string{"AWS_REGION", "AWS_DEFAULT_REGION"},
},
ForcesProviderReplace: tfbridge.True(),
},
"skip_region_validation": {
Default: &tfbridge.DefaultInfo{
Expand Down
2 changes: 2 additions & 0 deletions provider/test-programs/changing-region/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin/
/node_modules/
7 changes: 7 additions & 0 deletions provider/test-programs/changing-region/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: changing-region
runtime: nodejs
description: A test program to try changing AWS region
config:
pulumi:tags:
value:
pulumi:template: aws-typescript
23 changes: 23 additions & 0 deletions provider/test-programs/changing-region/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";

const cfg = new pulumi.Config();

export const desiredRegion: string|undefined = cfg.get("desired-region");

function parseRegion(region: string|undefined): aws.Region|undefined {
if (region === undefined) {
return undefined;
}
var r: any = region;
return r;
}

const opts: pulumi.ResourceOptions =
desiredRegion
? {provider: new aws.Provider("myprovider", {region: parseRegion(desiredRegion)})}
: {};

const queue = new aws.sqs.Queue("my-queue", {}, opts)

export const actualRegion = queue.arn.apply(x => x.split(":")[3]);
12 changes: 12 additions & 0 deletions provider/test-programs/changing-region/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "changing-region",
"main": "index.ts",
"devDependencies": {
"@types/node": "^18",
"typescript": "^5.0.0"
},
"dependencies": {
"@pulumi/aws": "^6.0.0",
"@pulumi/pulumi": "^3.113.0"
}
}
18 changes: 18 additions & 0 deletions provider/test-programs/changing-region/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2020",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
]
}

0 comments on commit bbb6123

Please sign in to comment.