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

chore: add example of looking up availability zones #248

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/examples_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,22 @@ func TestAPIWebsocketLambdaDynamoDB(t *testing.T) {
integration.ProgramTest(t, &test)
}

func TestLookupAzs(t *testing.T) {
test := getJSBaseOptions(t).
With(integration.ProgramTestOptions{
Dir: filepath.Join(getCwd(t), "lookup-azs"),
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
t.Helper()
t.Logf("Outputs: %v", stack.Outputs)
azs := stack.Outputs["azs"].([]interface{})
// by default the CDK will use 2 AZs so this makes sure our logic is working
assert.Lenf(t, azs, 3, "Expected 2 AZs, got %d", len(azs))
},
})

integration.ProgramTest(t, &test)
}

func getJSBaseOptions(t *testing.T) integration.ProgramTestOptions {
base := getBaseOptions(t)
baseJS := base.With(integration.ProgramTestOptions{
Expand Down
3 changes: 3 additions & 0 deletions examples/lookup-azs/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: pulumi-aws-cdk-lookups-azs
runtime: nodejs
description: lookup azs example for CDK
41 changes: 41 additions & 0 deletions examples/lookup-azs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as pulumicdk from '@pulumi/cdk';
import * as pulumi from '@pulumi/pulumi';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Fn } from 'aws-cdk-lib';
import { Output } from '@pulumi/pulumi';

const config = new pulumi.Config();
const prefix = config.get('prefix') ?? pulumi.getStack();
class LookupAzsStack extends pulumicdk.Stack {
public readonly azs: Output<string[]>;
constructor(app: pulumicdk.App, id: string) {
super(app, id, {
props: { env: app.env },
});

const vpc = new ec2.Vpc(this, 'MyVpc', { maxAzs: 3 });
this.azs = this.asOutput(vpc.availabilityZones);
}

// You can override the availabilityZones property to perform a lookup.
// Here I have specified that I want 3 availability zones. This uses Intrinsics, which
// behind the scenes are backed by Pulumi functions (e.g. aws_native.getAzs()).
// This allows us to get around the limitation of not being able to use Output values here.
get availabilityZones(): string[] {
Copy link
Contributor

@flostadler flostadler Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we could achieve CDKs behavior by building a small resource that takes an input property, but then just returns the value from state indefinitely (unless recreated).

Overkill for now, but maybe something for the future if we see this being annoying for users.

return [Fn.select(0, Fn.getAzs()), Fn.select(1, Fn.getAzs()), Fn.select(2, Fn.getAzs())];
}
}

class MyApp extends pulumicdk.App {
constructor() {
super('app', (scope: pulumicdk.App): pulumicdk.AppOutputs => {
const stack = new LookupAzsStack(scope, `${prefix}-azs`);
return {
azs: stack.azs,
};
});
}
}

const app = new MyApp();
export const azs = app.outputs['azs'];
18 changes: 18 additions & 0 deletions examples/lookup-azs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "pulumi-aws-cdk",
"devDependencies": {
"@types/node": "^10.0.0"
},
"dependencies": {
"@pulumi/aws": "^6.0.0",
"@pulumi/aws-native": "^1.9.0",
"@pulumi/cdk": "^0.5.0",
"@pulumi/docker-build": "^0.0.7",
"@pulumi/pulumi": "^3.0.0",
"@types/express": "^5.0.0",
"aws-cdk-lib": "2.156.0",
"constructs": "10.3.0",
"esbuild": "^0.24.0",
"express": "^4.21.1"
}
}
18 changes: 18 additions & 0 deletions examples/lookup-azs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
]
}