-
Notifications
You must be signed in to change notification settings - Fork 820
fix: populate custom resources CDK stacks with region and account (#12575) #13316
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
Open
bombguy
wants to merge
3
commits into
aws-amplify:dev
Choose a base branch
from
bombguy:12575
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
packages/amplify-category-custom/src/__tests__/utils/generate-cfn-from-cdk.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getCDKProps } from '../../utils/generate-cfn-from-cdk'; | ||
|
||
describe('generate-cfn-from-cdk', () => { | ||
describe('getCDKProps', () => { | ||
beforeEach(() => { | ||
process.env = {}; | ||
}); | ||
|
||
test('should return undefined if CDK env vars are not set', () => { | ||
const actual = getCDKProps(); | ||
expect(Object.values(actual)).toHaveLength(0); | ||
}); | ||
|
||
test('should return cdk props with env set', () => { | ||
process.env = { | ||
CDK_DEFAULT_ACCOUNT: 'some_account_id', | ||
CDK_DEFAULT_REGION: 'us-east-1', | ||
}; | ||
|
||
const actual = getCDKProps(); | ||
|
||
expect(actual.env?.account).toEqual('some_account_id'); | ||
expect(actual.env?.region).toEqual('us-east-1'); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/amplify-e2e-tests/custom-resources/barebone-cdk-stack.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as cdk from 'aws-cdk-lib'; | ||
import * as sqs from 'aws-cdk-lib/aws-sqs'; | ||
import * as sns from 'aws-cdk-lib/aws-sns'; | ||
import * as subs from 'aws-cdk-lib/aws-sns-subscriptions'; | ||
import * as ec2 from 'aws-cdk-lib/aws-ec2'; | ||
import { Construct } from 'constructs'; | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import * as AmplifyHelpers from '@aws-amplify/cli-extensibility-helper'; | ||
|
||
export class cdkStack extends cdk.Stack { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
constructor(scope: Construct, id: string, props?: cdk.StackProps, amplifyResourceProps?: AmplifyHelpers.AmplifyResourceProps) { | ||
super(scope, id, props); | ||
/* Do not remove - Amplify CLI automatically injects the current deployment environment in this input parameter */ | ||
new cdk.CfnParameter(this, 'env', { | ||
type: 'String', | ||
description: 'Current Amplify CLI env name', | ||
}); | ||
/* AWS CDK code goes here - learn more: https://docs.aws.amazon.com/cdk/latest/guide/home.html */ | ||
|
||
ec2.Vpc.fromLookup(this, 'VPC', { | ||
isDefault: true, | ||
}); | ||
|
||
const queue = new sqs.Queue(this, 'sqs-queue'); | ||
// 👇 create sns topic | ||
const topic = new sns.Topic(this, 'sns-topic'); | ||
|
||
// 👇 subscribe queue to topic | ||
topic.addSubscription(new subs.SqsSubscription(queue)); | ||
|
||
// creation of resources inside the VPC | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -18,6 +18,7 @@ import { v4 as uuid } from 'uuid'; | |||||||||||||||
|
||||||||||||||||
describe('adding custom resources test', () => { | ||||||||||||||||
let projRoot: string; | ||||||||||||||||
|
||||||||||||||||
beforeEach(async () => { | ||||||||||||||||
projRoot = await createNewProjectDir('custom-resources'); | ||||||||||||||||
}); | ||||||||||||||||
|
@@ -130,4 +131,23 @@ describe('adding custom resources test', () => { | |||||||||||||||
}, | ||||||||||||||||
}); | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
it('should use default cdk env var if set', async () => { | ||||||||||||||||
const cdkResourceName = `custom${uuid().split('-')[0]}`; | ||||||||||||||||
const destCustomResourceFilePath = path.join(projRoot, 'amplify', 'backend', 'custom', cdkResourceName, 'cdk-stack.ts'); | ||||||||||||||||
const srcCustomResourceFilePath = path.join(__dirname, '..', '..', 'custom-resources', 'barebone-cdk-stack.ts'); | ||||||||||||||||
|
||||||||||||||||
await initJSProjectWithProfile(projRoot, {}); | ||||||||||||||||
await addCDKCustomResource(projRoot, { name: cdkResourceName }); | ||||||||||||||||
|
||||||||||||||||
fs.copyFileSync(srcCustomResourceFilePath, destCustomResourceFilePath); | ||||||||||||||||
|
||||||||||||||||
// no aws account & region set, should throw error, | ||||||||||||||||
await expect(amplifyPushAuth(projRoot)).rejects.toThrowError(); | ||||||||||||||||
|
||||||||||||||||
// should pickup to default cdk env vars and build cfn stack. | ||||||||||||||||
process.env.CDK_DEFAULT_ACCOUNT = 'some_id'; | ||||||||||||||||
process.env.CDK_DEFAULT_REGION = process.env.CLI_REGION; | ||||||||||||||||
await amplifyPushAuth(projRoot); | ||||||||||||||||
Comment on lines
+148
to
+151
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. this lacks assertion. amplify-cli/packages/amplify-e2e-tests/overrides/override-auth.ts Lines 15 to 21 in d60e505
Can we please use similar pattern in barebone-cdk-stack.ts ?
|
||||||||||||||||
}); | ||||||||||||||||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.