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

Enable autoTrim behavior by default #227

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ Try the workshop at https://apprunnerworkshop.com
Read the docs at https://docs.aws.amazon.com/apprunner
```

## AWS Cloud Control AutoNaming Config

Sometimes CDK constructs can create resource names that are too long for the
[AWS Cloud Control provider](https://www.pulumi.com/registry/packages/aws-native/).
When this happens you can configure the `autoTrim` feature to have the generated
names be automatically trimmed to fit within the name requirements. If you are
not configuring your own `aws-native` provider then this feature is enabled by
default. If you _are_ configuring your own `aws-native` provider then you will
have to enable this.

```ts
const nativeProvider = new aws_native.Provider('cdk-native-provider', {
region: 'us-east-2',
autoNaming: {
autoTrim: true,
randomSuffixMinLength: 7,
},
});
const app = new pulumicdk.App('app', (scope: pulumicdk.App): pulumicdk.AppOutputs => {
const stack = new AppRunnerStack('teststack');
return {
url: stack.url,
};
}, {
providers: [ nativeProvider ],
});
```

## Bootstrapping

CDK has the concept of [bootstrapping](https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html)
Expand Down
7 changes: 1 addition & 6 deletions examples/alb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
import * as pulumi from '@pulumi/pulumi';
import * as pulumicdk from '@pulumi/cdk';
import { CfnOutput } from 'aws-cdk-lib';

class AlbStack extends pulumicdk.Stack {
url: pulumi.Output<string>;
Expand All @@ -28,15 +27,11 @@ class AlbStack extends pulumicdk.Stack {
port: 80,
});

const tg = listener.addTargets('Target', {
listener.addTargets('Target', {
port: 80,
targets: [asg],
});

// workaround for https://github.com/pulumi/pulumi-cdk/issues/62
const cfnTargetGroup = tg.node.defaultChild as elbv2.CfnTargetGroup;
cfnTargetGroup.overrideLogicalId('LBListenerTG');

listener.connections.allowDefaultPortFromAnyIpv4('Open to the world');

asg.scaleOnRequestCount('AModestLoad', {
Expand Down
6 changes: 0 additions & 6 deletions integration/apigateway/sfn-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as iam from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as apigw from 'aws-cdk-lib/aws-apigateway';
Expand All @@ -15,13 +14,8 @@ export class SfnApi extends Construct {
stateMachineType: sfn.StateMachineType.EXPRESS,
});

// TODO[pulumi/pulumi-cdk#62] The auto created role has too long name
const role = new iam.Role(this, 'StartRole', {
assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com'),
});
const api = new apigw.StepFunctionsRestApi(this, 'StepFunctionsRestApi', {
deploy: false,
role,
stateMachine: stateMachine,
headers: true,
path: false,
Expand Down
2 changes: 0 additions & 2 deletions integration/examples_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ func TestCustomResource(t *testing.T) {
test := getJSBaseOptions(t).
With(integration.ProgramTestOptions{
Dir: filepath.Join(getCwd(t), "custom-resource"),
// Workaround until TODO[pulumi/pulumi-aws-native#1816] is resolved.
Env: []string{"PULUMI_CDK_EXPERIMENTAL_MAX_NAME_LENGTH=56"},
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
t.Logf("Outputs: %v", stack.Outputs)
url := stack.Outputs["websiteUrl"].(string)
Expand Down
13 changes: 1 addition & 12 deletions src/cfn-resource-mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,7 @@ export function mapToCfnResource(
const pType = pulumiTypeName(typeName);
const awsModule = aws as any;

// Workaround until TODO[pulumi/pulumi-aws-native#1816] is resolved.
// Not expected to be exposed to users
let name = logicalId;
const maxNameLength = process.env.PULUMI_CDK_EXPERIMENTAL_MAX_NAME_LENGTH;
if (maxNameLength) {
const maxLength = parseInt(maxNameLength, 10);
if (name.length > maxLength) {
name = name.substring(0, maxLength);
}
}

return new awsModule[mName][pType](name, props, options);
return new awsModule[mName][pType](logicalId, props, options);
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { PulumiSynthesizer, PulumiSynthesizerBase } from './synthesizer';
import { AwsCdkCli, ICloudAssemblyDirectoryProducer } from '@aws-cdk/cli-lib-alpha';
import { CdkConstruct } from './interop';
import { makeUniqueId } from './cdk-logical-id';
import * as native from '@pulumi/aws-native';

export type AppOutputs = { [outputId: string]: pulumi.Output<any> };

Expand Down Expand Up @@ -93,7 +94,24 @@ export class App
private appProps?: cdk.AppProps;

constructor(id: string, createFunc: (scope: App) => void | AppOutputs, props?: AppResourceOptions) {
super('cdk:index:App', id, props?.appOptions, props);
// This matches the logic found in aws-native. If all of these are undefined the provider
// will throw an error
const region = native.config.region ?? process.env.AWS_REGION ?? process.env.AWS_DEFAULT_REGION!;
// If the user has not provided any providers, we will create an aws-native one by default in order
// to enable the autoNaming feature.
const providers = props?.providers ?? [
Copy link
Member

Choose a reason for hiding this comment

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

This could be a bit more precise maybe? If props resets the classic provider, it might still be able to merge in the config for the native provider? Probably a rare use case though, feel free to disregard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's a good call. I've updated it to handle that case.

new native.Provider('cdk-aws-native', {
region: region as native.Region,
autoNaming: {
randomSuffixMinLength: 7,
autoTrim: true,
},
}),
];
super('cdk:index:App', id, props?.appOptions, {
...props,
providers,
});
this.appOptions = props?.appOptions;
this.createFunc = createFunc;
this.component = this;
Expand Down
7 changes: 7 additions & 0 deletions tests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import { Vpc } from 'aws-cdk-lib/aws-ec2';
import { aws_ssm } from 'aws-cdk-lib';
import { Construct } from 'constructs';

beforeAll(() => {
process.env.AWS_REGION = 'us-east-2';
});
afterAll(() => {
process.env.AWS_REGION = undefined;
});

describe('Basic tests', () => {
setMocks();
test('Checking single resource registration', async () => {
Expand Down
4 changes: 4 additions & 0 deletions tests/cdk-resource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ import { Construct } from 'constructs';
describe('CDK Construct tests', () => {
let resources: MockResourceArgs[] = [];
beforeAll(() => {
process.env.AWS_REGION = 'us-east-2';
resources = [];
setMocks(resources);
});
afterAll(() => {
process.env.AWS_REGION = undefined;
});
// DynamoDB table was previously mapped to the `aws` provider
// otherwise this level of testing wouldn't be necessary.
// We also don't need to do this type of testing for _every_ resource
Expand Down
20 changes: 20 additions & 0 deletions tests/synthesizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import { asNetworkMode, asPlatforms } from '../src/synthesizer';
import { NetworkMode, Platform as DockerPlatform } from '@pulumi/docker-build';
import { DockerImageAsset, Platform, NetworkMode as Network } from 'aws-cdk-lib/aws-ecr-assets';

beforeAll(() => {
process.env.AWS_REGION = 'us-east-2';
});
afterAll(() => {
process.env.AWS_REGION = undefined;
});

describe('Synthesizer File Assets', () => {
test('no assets = no staging resources', async () => {
const resources: MockResourceArgs[] = [];
Expand All @@ -16,6 +23,19 @@ describe('Synthesizer File Assets', () => {
new CfnBucket(scope, 'Bucket');
});
expect(resources).toEqual([
expect.objectContaining({
inputs: {
autoNaming: '{"randomSuffixMinLength":7,"autoTrim":true}',
region: 'us-east-2',
skipCredentialsValidation: 'true',
skipGetEc2Platforms: 'true',
skipMetadataApiCheck: 'true',
skipRegionValidation: 'true',
},
name: 'cdk-aws-native',
provider: '',
type: 'pulumi:providers:aws-native',
}),
expect.objectContaining({
name: 'staging-stack-project-stack',
type: 'cdk:construct:StagingStack',
Expand Down
Loading