-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add ability to specify resource options at the stack level #245
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,6 +248,85 @@ const app = new pulumicdk.App('app', (scope: pulumicdk.App) => { | |
}); | ||
``` | ||
|
||
### Stack Level Providers | ||
|
||
It is also possible to customize the Providers at the Stack level. This can be | ||
useful in cases where you need to deploy resources to different AWS regions. | ||
|
||
```ts | ||
const awsProvider = new aws.Provider('aws-provider'); | ||
const awsCCAPIProvider = new ccapi.Provider('ccapi-provider', { | ||
// enable autoNaming | ||
autoNaming: { | ||
autoTrim: true, | ||
randomSuffixMinLength: 7, | ||
} | ||
}); | ||
|
||
const app = new pulumicdk.App('app', (scope: pulumicdk.App) => { | ||
// inherits the provider from the app | ||
const defaultProviderStack = new pulumicdk.Stack('default-provider-stack'); | ||
const bucket = new s3.Bucket(defaultProviderStack, 'Bucket'); | ||
|
||
// use a different provider for this stack | ||
const east2Stack = new pulumicdk.Stack('east2-stack', { | ||
providers: [ | ||
new aws.Provider('east2-provider', { region: 'us-east-2' }), | ||
new ccapi.Provider('east2-ccapi-provider', { | ||
region: 'us-east-2', | ||
autoNaming: { | ||
autoTrim: true, | ||
randomSuffixMinLength: 7, | ||
}, | ||
}), | ||
], | ||
}); | ||
const bucket2 = new s3.Bucket(east2Stack, 'Bucket'); | ||
}, { | ||
providers: [ | ||
dockerBuildProvider, | ||
awsProvider, | ||
awsCCAPIProvider, | ||
] | ||
}); | ||
``` | ||
|
||
One thing to note is that when you pass different custom providers to a Stack, | ||
by default the Stack becomes an [environment agnostic stack](https://docs.aws.amazon.com/cdk/v2/guide/configure-env.html#configure-env-examples). | ||
If you want to have the environment specified at the CDK Stack level, then you | ||
also need to provide the environment to the Stack Props. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
```ts | ||
const app = new pulumicdk.App('app', (scope: pulumicdk.App) => { | ||
// inherits the provider from the app and has the CDK env auto populated | ||
// based on the default provider | ||
const defaultProviderStack = new pulumicdk.Stack('default-provider-stack'); | ||
const bucket = new s3.Bucket(defaultProviderStack, 'Bucket'); | ||
|
||
// use a different provider for this stack | ||
const east2Stack = new pulumicdk.Stack('east2-stack', { | ||
props: { | ||
env: { | ||
region: 'us-east-2', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I understand now the comment you had regarding not being able to pass a pulumi.Output region in here automatically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was looking at IResolvable but it's unclear that that's allowed to return promises to CDK. |
||
account: '12345678912', | ||
}, | ||
}, | ||
providers: [ | ||
new aws.Provider('east2-provider', { region: 'us-east-2' }), | ||
new ccapi.Provider('east2-ccapi-provider', { | ||
region: 'us-east-2', | ||
autoNaming: { | ||
autoTrim: true, | ||
randomSuffixMinLength: 7, | ||
}, | ||
}), | ||
], | ||
}); | ||
const bucket2 = new s3.Bucket(east2Stack, 'Bucket'); | ||
}); | ||
|
||
``` | ||
|
||
## CDK Lookups | ||
|
||
CDK [lookups](https://docs.aws.amazon.com/cdk/v2/guide/context.html#context_methods) are currently disabled by default. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
name: pulumi-stack-provider | ||
runtime: nodejs | ||
description: A minimal TypeScript Pulumi program |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import * as logs from 'aws-cdk-lib/aws-logs'; | ||
import * as pulumi from '@pulumi/pulumi'; | ||
import * as pulumicdk from '@pulumi/cdk'; | ||
import * as native from '@pulumi/aws-native'; | ||
import { RemovalPolicy } from 'aws-cdk-lib'; | ||
|
||
const config = new pulumi.Config(); | ||
const cdkRegion = config.get('cdk-region'); | ||
const cdkAccount = config.get('cdk-account'); | ||
const defaultRegion = config.get('default-region'); | ||
|
||
export class StackProviderStack extends pulumicdk.Stack { | ||
public readonly logsRegion: pulumi.Output<string>; | ||
constructor(app: pulumicdk.App, id: string, providers?: pulumi.ProviderResource[]) { | ||
super(app, id, { | ||
providers, | ||
props: | ||
cdkRegion || cdkAccount | ||
? { | ||
env: { | ||
region: cdkRegion, | ||
account: cdkAccount, | ||
}, | ||
} | ||
: undefined, | ||
}); | ||
|
||
const group = new logs.LogGroup(this, 'group', { | ||
retention: logs.RetentionDays.ONE_DAY, | ||
removalPolicy: RemovalPolicy.DESTROY, | ||
}); | ||
|
||
this.logsRegion = this.asOutput(group.logGroupArn).apply((arn) => arn.split(':')[3]); | ||
} | ||
} | ||
|
||
const app = new pulumicdk.App( | ||
'app', | ||
(scope: pulumicdk.App) => { | ||
const stack = new StackProviderStack(scope, 'teststack', [ | ||
new native.Provider('ccapi-provider', { | ||
region: 'us-east-1', // a different region from the app provider | ||
}), | ||
]); | ||
const defaultStack = new StackProviderStack(scope, 'default-stack'); | ||
return { | ||
east1LogsRegion: stack.logsRegion, | ||
east1StackRegion: stack.asOutput(stack.region), | ||
defaultLogsRegion: defaultStack.logsRegion, | ||
defaultStackRegion: defaultStack.asOutput(defaultStack.region), | ||
}; | ||
}, | ||
{ | ||
providers: defaultRegion | ||
? [ | ||
new native.Provider('app-provider', { | ||
region: defaultRegion as native.Region, // a different region from the default env | ||
}), | ||
] | ||
: undefined, | ||
}, | ||
); | ||
|
||
// You can (we check for this though) configure a different region on the provider | ||
// that the stack uses vs the region in the CDK StackProps. This tests checks that both the | ||
// stack region and the region the resources are deployed to are the same. | ||
export const east1LogsRegion = app.outputs['east1LogsRegion']; | ||
export const defaultLogsRegion = app.outputs['defaultLogsRegion']; | ||
export const east1StackRegion = app.outputs['east1StackRegion']; | ||
export const defaultStackRegion = app.outputs['defaultStackRegion']; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"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", | ||
"aws-cdk-lib": "2.156.0", | ||
"constructs": "10.3.0" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
examples are better with imports I think, if not too hard. To see where
aws
is coming from.