Skip to content

Commit

Permalink
feat: migrate CLI to CDK v2 (#10988)
Browse files Browse the repository at this point in the history
* feat: converts auth and storage categories to cdk V2

* fix: fixes auth unit test

* fix: minor fixes in categories

* fix: increase node memory usage

* fix: removes invalid import

* fix: fixes custom resource test

* chore: turn the auto-populate owner field FF ON for new projects (#11230)

* fix: converts all categories to v2

* chore: added linting

* fix: fixes unit tests

* chore: address comments

* chore: fixes ordring in package.json

* chore: minor fixes

* chore: reverts unused imports and merge changes

Co-authored-by: Akshay Upadhyay <[email protected]>
Co-authored-by: Phani Srikar Edupuganti <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2022
1 parent 592fd90 commit 7fd3bc1
Show file tree
Hide file tree
Showing 54 changed files with 415 additions and 1,035 deletions.
2 changes: 1 addition & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
"analyzeSourceFiles": false
}
}
}
}
6 changes: 2 additions & 4 deletions packages/amplify-category-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@
"dependencies": {
"@aws-amplify/amplify-environment-parameters": "1.1.3",
"@aws-amplify/cli-extensibility-helper": "2.4.3",
"@aws-cdk/aws-cognito": "^1.159.0",
"@aws-cdk/aws-iam": "^1.159.0",
"@aws-cdk/aws-lambda": "^1.159.0",
"@aws-cdk/core": "^1.159.0",
"amplify-cli-core": "3.2.2",
"amplify-headless-interface": "1.15.0",
"amplify-prompts": "2.6.0",
"amplify-util-headless-input": "1.9.6",
"amplify-util-import": "2.2.36",
"aws-cdk-lib": "~2.44.0",
"aws-sdk": "^2.1233.0",
"chalk": "^4.1.1",
"change-case": "^4.1.1",
"constructs": "^10.0.5",
"enquirer": "^2.3.6",
"fs-extra": "^8.1.0",
"inquirer": "^7.3.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable max-lines-per-function */
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import * as cdk from 'aws-cdk-lib';
import * as iam from 'aws-cdk-lib/aws-iam';
import { AmplifyAuthCognitoStack } from '../../../../provider-utils/awscloudformation/auth-stack-builder/auth-cognito-stack-builder';
import { AuthStackSynthesizer } from '../../../../provider-utils/awscloudformation/auth-stack-builder/stack-synthesizer';
import { AttributeType } from '../../../../provider-utils/awscloudformation/service-walkthrough-types/awsCognito-user-input-types';
import { CognitoStackOptions } from '../../../../provider-utils/awscloudformation/service-walkthrough-types/cognito-user-input-types';
import { AttributeType, CognitoStackOptions } from '../../../../provider-utils/awscloudformation/service-walkthrough-types/cognito-user-input-types';

describe('generateCognitoStackResources', () => {
const props: CognitoStackOptions = {
Expand Down Expand Up @@ -83,7 +80,7 @@ describe('generateCognitoStackResources', () => {
expect(cognitoStack.oAuthCustomResource).toBeDefined();
expect(
cognitoStack
.oAuthCustomResource!.node!.dependencies!.map((dep: any) => dep.target.logicalId)
.oAuthCustomResource!.node!.dependencies!.map((dep: any) => dep.logicalId)
.map(logicalIdToken => /testCognitoStack\.([^.]+)\.Default/.exec(logicalIdToken)![1]),
).toMatchInlineSnapshot(`
Array [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as cognito from 'aws-cdk-lib/aws-cognito';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { AmplifyAuthCognitoStackTemplate } from '@aws-amplify/cli-extensibility-helper';
import * as cognito from '@aws-cdk/aws-cognito';
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
// eslint-disable-next-line import/no-extraneous-dependencies
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
import { $TSAny, JSONUtilities } from 'amplify-cli-core';
import * as fs from 'fs-extra';
import _ from 'lodash';
import { Construct } from 'constructs';
import {
hostedUILambdaFilePath,
hostedUIProviderLambdaFilePath,
Expand All @@ -16,8 +16,8 @@ import {
openIdLambdaFilePath,
userPoolClientLambdaFilePath,
} from '../constants';
import { AttributeType } from '../service-walkthrough-types/awsCognito-user-input-types';
import { CognitoStackOptions } from '../service-walkthrough-types/cognito-user-input-types';
import { AttributeType } from '../service-walkthrough-types/awsCognito-user-input-types';

const CFN_TEMPLATE_FORMAT_VERSION = '2010-09-09';
const ROOT_CFN_DESCRIPTION = 'Amplify Cognito Stack for AWS Amplify CLI';
Expand Down Expand Up @@ -53,7 +53,7 @@ export type AmplifyAuthCognitoStackProps = {
* L2 construct for amplify auth cognito stack
*/
export class AmplifyAuthCognitoStack extends cdk.Stack implements AmplifyAuthCognitoStackTemplate {
private _scope: cdk.Construct;
private _scope: Construct;
private _cfnParameterMap: Map<string, cdk.CfnParameter> = new Map();
private _cfnConditionMap: Map<string, cdk.CfnCondition> = new Map();
private _cfnOutputMap: Map<string, cdk.CfnOutput> = new Map();
Expand Down Expand Up @@ -103,7 +103,7 @@ export class AmplifyAuthCognitoStack extends cdk.Stack implements AmplifyAuthCog
openIdLambdaInputs?: cdk.CustomResource;
openIdLambdaRole?: iam.CfnRole;

constructor(scope: cdk.Construct, id: string, props: AmplifyAuthCognitoStackProps) {
constructor(scope: Construct, id: string, props: AmplifyAuthCognitoStackProps) {
super(scope, id, props);
this._scope = scope;
this.templateOptions.templateFormatVersion = CFN_TEMPLATE_FORMAT_VERSION;
Expand Down Expand Up @@ -536,7 +536,7 @@ export class AmplifyAuthCognitoStack extends cdk.Stack implements AmplifyAuthCog
&& !_.isEmpty(props.authProviders)
&& !(Object.keys(props.authProviders).length === 1 && props.authProviders[0] === 'accounts.google.com' && props.audiences)
) {
this.identityPool.supportedLoginProviders = cdk.Lazy.anyValue({
this.identityPool.supportedLoginProviders = cdk.Lazy.any({
produce: () => {
const supportedProvider: $TSAny = {};
props.authProviders?.forEach(provider => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
/* eslint-disable max-lines-per-function */
import * as cdk from '@aws-cdk/core';
import {
$TSAny, $TSContext, AmplifyCategories, AmplifyCategoryTransform,
AmplifyStackTemplate, AmplifySupportedService, buildOverrideDir,
CFNTemplateFormat, FeatureFlags, JSONUtilities, pathManager,
stateManager, Template, writeCFNTemplate,
$TSAny,
$TSContext,
AmplifyCategories,
AmplifyCategoryTransform,
AmplifyStackTemplate,
AmplifySupportedService,
buildOverrideDir,
CFNTemplateFormat,
FeatureFlags,
JSONUtilities,
pathManager,
stateManager,
Template,
writeCFNTemplate
} from 'amplify-cli-core';
import { formatter, printer } from 'amplify-prompts';
import {
formatter,
printer
} from 'amplify-prompts';
import * as cdk from 'aws-cdk-lib';
import * as fs from 'fs-extra';
import _ from 'lodash';
import os from 'os';
Expand All @@ -17,7 +29,8 @@ import { AttributeType, CognitoCLIInputs } from '../service-walkthrough-types/aw
import { AuthTriggerConnection, AuthTriggerPermissions, CognitoStackOptions } from '../service-walkthrough-types/cognito-user-input-types';
import { generateNestedAuthTriggerTemplate } from '../utils/generate-auth-trigger-template';
import { createUserPoolGroups, updateUserPoolGroups } from '../utils/synthesize-resources';
import { AmplifyAuthCognitoStack, AuthStackSynthesizer } from './index';
import { AmplifyAuthCognitoStack } from './auth-cognito-stack-builder';
import { AuthStackSynthesizer } from './stack-synthesizer';

/**
* Class to handle Auth cdk generation / override functionality
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable max-classes-per-file */
import * as cdk from 'aws-cdk-lib';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { CfnUserPoolGroup } from 'aws-cdk-lib/aws-cognito';
import { AmplifyUserPoolGroupStackTemplate } from '@aws-amplify/cli-extensibility-helper';
import { CfnUserPoolGroup } from '@aws-cdk/aws-cognito';
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import { JSONUtilities } from 'amplify-cli-core';
import * as fs from 'fs-extra';
import { Construct } from 'constructs';
// eslint-disable-next-line import/no-cycle
import { roleMapLambdaFilePath } from '../constants';
// eslint-disable-next-line import/no-cycle
Expand All @@ -25,7 +26,7 @@ export type AmplifyAuthCognitoStackProps = {
* CDK stack that contains the UserPool Group resources
*/
export class AmplifyUserPoolGroupStack extends cdk.Stack implements AmplifyUserPoolGroupStackTemplate {
_scope: cdk.Construct;
_scope: Construct;
private _cfnParameterMap: Map<string, cdk.CfnParameter> = new Map();
private _cfnConditionMap: Map<string, cdk.CfnCondition> = new Map();
userPoolGroup: Record<string, CfnUserPoolGroup>;
Expand All @@ -34,7 +35,7 @@ export class AmplifyUserPoolGroupStack extends cdk.Stack implements AmplifyUserP
roleMapLambdaFunction?: lambda.CfnFunction;
lambdaExecutionRole?: iam.CfnRole;

constructor(scope: cdk.Construct, id: string, props: AmplifyAuthCognitoStackProps) {
constructor(scope: Construct, id: string, props: AmplifyAuthCognitoStackProps) {
super(scope, id, props);
this._scope = scope;
this.templateOptions.templateFormatVersion = CFN_TEMPLATE_FORMAT_VERSION;
Expand Down Expand Up @@ -312,7 +313,7 @@ const getCfnParamsLogicalId = (cognitoResourceName: string, cfnParamName: string
*/
export class AmplifyUserPoolGroupStackOutputs extends cdk.Stack {
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
constructor(scope: cdk.Construct, id: string, props: AmplifyAuthCognitoStackProps) {
constructor(scope: Construct, id: string, props: AmplifyAuthCognitoStackProps) {
super(scope, id, props);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ISynthesisSession, Stack, LegacyStackSynthesizer } from '@aws-cdk/core';
import { JSONUtilities } from 'amplify-cli-core';
import type { Template } from 'cloudform-types';
import { AmplifyAuthCognitoStack, AmplifyUserPoolGroupStack, AmplifyUserPoolGroupStackOutputs } from './index';
import { ISynthesisSession, Stack, LegacyStackSynthesizer } from 'aws-cdk-lib';
import { Template } from 'cloudform-types';
import { AmplifyAuthCognitoStack } from './auth-cognito-stack-builder';
import { AmplifyUserPoolGroupStack, AmplifyUserPoolGroupStackOutputs } from './auth-user-pool-group-stack-builder';

/**
* Amplify Stack Synthesizer for Auth Category
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as cdk from '@aws-cdk/core';
import {
$TSAny, $TSContext,
AmplifyCategories, AmplifyCategoryTransform, AmplifyStackTemplate, AmplifySupportedService,
buildOverrideDir,
CFNTemplateFormat,
JSONUtilities,
pathManager, Template, writeCFNTemplate,
pathManager, Template, writeCFNTemplate
} from 'amplify-cli-core';
import { formatter, printer } from 'amplify-prompts';
import * as cdk from 'aws-cdk-lib';
import * as fs from 'fs-extra';
import os from 'os';
import * as path from 'path';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as path from 'path';
import * as fs from 'fs-extra';
import { JSONUtilities, pathManager } from 'amplify-cli-core';
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import { prepareApp } from '@aws-cdk/core/lib/private/prepare-app';
import { Construct, CustomResource } from '@aws-cdk/core';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as cdk from 'aws-cdk-lib';
import { CustomResource } from 'aws-cdk-lib';
import { v4 as uuid } from 'uuid';
import { Construct } from 'constructs';
import { AuthTriggerConnection, CognitoStackOptions } from '../service-walkthrough-types/cognito-user-input-types';
import { authTriggerAssetFilePath } from '../constants';

Expand All @@ -21,7 +21,7 @@ const CFN_TEMPLATE_FORMAT_VERSION = '2010-09-09';
* CDK stack for custom auth resources
*/
export class CustomResourceAuthStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props: CustomResourceAuthStackProps) {
constructor(scope: Construct, id: string, props: CustomResourceAuthStackProps) {
super(scope, id, props);
this.templateOptions.templateFormatVersion = CFN_TEMPLATE_FORMAT_VERSION;

Expand Down Expand Up @@ -61,7 +61,6 @@ export class CustomResourceAuthStack extends cdk.Stack {
* Generates a CFN template from the CDK stack
*/
toCloudFormation(): Record<string, unknown> {
prepareApp(this);
return this._toCloudFormation();
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/amplify-category-custom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"amplify-cli-core": "3.2.2",
"amplify-prompts": "2.6.0",
"aws-cdk-lib": "~2.44.0",
"execa": "^5.1.1",
"fs-extra": "^8.1.0",
"glob": "^7.2.0",
Expand Down Expand Up @@ -57,4 +58,4 @@
"!**/*.d.ts"
]
}
}
}
10 changes: 5 additions & 5 deletions packages/amplify-category-custom/resources/cdk-stack.ts.sample
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as cdk from '@aws-cdk/core';
import * as cdk from 'aws-cdk-lib';
import * as AmplifyHelpers from '@aws-amplify/cli-extensibility-helper';
import { AmplifyDependentResourcesAttributes } from '../../types/amplify-dependent-resources-ref';
//import * as iam from '@aws-cdk/aws-iam';
//import * as sns from '@aws-cdk/aws-sns';
//import * as subs from '@aws-cdk/aws-sns-subscriptions';
//import * as sqs from '@aws-cdk/aws-sqs';
//import * as iam from 'aws-cdk-lib/aws-iam';
//import * as sns from 'aws-cdk-lib/aws-sns';
//import * as subs from 'aws-cdk-lib/aws-sns-subscriptions';
//import * as sqs from 'aws-cdk-lib/aws-sqs';

export class cdkStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps, amplifyResourceProps?: AmplifyHelpers.AmplifyResourceProps) {
Expand Down
6 changes: 1 addition & 5 deletions packages/amplify-category-custom/resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
},
"dependencies": {
"@aws-amplify/cli-extensibility-helper": "^2.0.0",
"@aws-cdk/core": "^1.159.0",
"@aws-cdk/aws-iam": "^1.159.0",
"@aws-cdk/aws-sns-subscriptions": "^1.159.0",
"@aws-cdk/aws-sqs": "^1.159.0",
"@aws-cdk/aws-sns": "^1.159.0"
"aws-cdk-lib": "~2.44.0"
},
"devDependencies": {
"typescript": "^4.2.4"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { $TSContext, CFNTemplateFormat, readCFNTemplate, pathManager, stateManager, writeCFNTemplate } from 'amplify-cli-core';
import {
$TSContext, CFNTemplateFormat, readCFNTemplate, pathManager, stateManager, writeCFNTemplate,
} from 'amplify-cli-core';
import { glob } from 'glob';
import * as inquirer from 'inquirer';
import { prompter } from 'amplify-prompts';
import * as fs from 'fs-extra';
import * as cdk from 'aws-cdk-lib';
import {
getResourceCfnOutputAttributes,
getAllResources,
addCDKResourceDependency,
addCFNResourceDependency,
} from '../../utils/dependency-management-utils';
import * as cdk from '@aws-cdk/core';

jest.mock('amplify-cli-core');
jest.mock('amplify-prompts');
Expand Down Expand Up @@ -319,7 +321,7 @@ describe('addCFNResourceDependency() scenarios', () => {
} as unknown as $TSContext;
});

it('add new resource dependency to custom cfn stack ', async () => {
it('add new resource dependency to custom cfn stack', async () => {
prompter.yesOrNo = jest.fn().mockReturnValueOnce(true);
fs_mock.existsSync.mockReturnValue(false); // if build dir exists

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as cdk from '@aws-cdk/core';
import * as cdk from 'aws-cdk-lib';
import { $TSContext, $TSObject, pathManager, readCFNTemplate, stateManager, writeCFNTemplate } from 'amplify-cli-core';
import { printer, prompter } from 'amplify-prompts';
import * as fs from 'fs-extra';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as cdk from '@aws-cdk/core';
import * as cdk from 'aws-cdk-lib';
import { JSONUtilities, pathManager } from 'amplify-cli-core';
import * as path from 'path';
import { categoryName } from './constants';
Expand Down
5 changes: 2 additions & 3 deletions packages/amplify-category-geo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
"aws"
],
"dependencies": {
"@aws-cdk/aws-iam": "^1.159.0",
"@aws-cdk/aws-lambda": "^1.159.0",
"@aws-cdk/core": "^1.159.0",
"ajv": "^6.12.6",
"amplify-cli-core": "3.2.2",
"amplify-headless-interface": "1.15.0",
"amplify-prompts": "2.6.0",
"amplify-util-headless-input": "1.9.6",
"aws-cdk-lib": "~2.44.0",
"aws-sdk": "^2.1233.0",
"constructs": "^10.0.5",
"fs-extra": "^8.1.0",
"lodash": "^4.17.21",
"uuid": "^8.3.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ Object {
"UpdateReplacePolicy": "Delete",
},
"CustomMapLambda51D5D430": Object {
"DependsOn": Array [
"CustomMapLambdaServiceRoleDefaultPolicy983FDD4E",
"CustomMapLambdaServiceRole4EE7732C",
],
"Properties": Object {
"Code": Object {
"ZipFile": "const response = require('cfn-response');
Expand Down Expand Up @@ -457,10 +453,6 @@ Object {
"UpdateReplacePolicy": "Delete",
},
"CustomMapLambda51D5D430": Object {
"DependsOn": Array [
"CustomMapLambdaServiceRoleDefaultPolicy983FDD4E",
"CustomMapLambdaServiceRole4EE7732C",
],
"Properties": Object {
"Code": Object {
"ZipFile": "const response = require('cfn-response');
Expand Down
Loading

0 comments on commit 7fd3bc1

Please sign in to comment.