Skip to content

Commit

Permalink
fix: update custom cdk seleton templates + format override skeleton f…
Browse files Browse the repository at this point in the history
…ile (aws-amplify#8752)

* fix: update custom cdk seleton tempaltes + format override skeleton file

* chore: revert package version chnge for cli-core

Co-authored-by: Ghosh <kaustavg@3c22fb229ff6.ant.amazon.com>
  • Loading branch information
2 people authored and Sachin Panemangalore committed Nov 10, 2021
1 parent efe8932 commit 8bff3dc
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AmplifyAuthCognitoStackTemplate } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyAuthCognitoStackTemplate) {}
export function override(resources: AmplifyAuthCognitoStackTemplate) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@aws-amplify/cli-extensibility-helper": "1.0.1-ext15.0"
"@aws-amplify/cli-extensibility-helper": "latest"
},
"devDependencies": {
"typescript": "^4.2.4"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import * as cdk from '@aws-cdk/core';
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';
*/


export class cdkStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
constructor(scope: cdk.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 paramater */
Expand All @@ -16,13 +19,24 @@ export class cdkStack extends cdk.Stack {
});

/* AWS CDK Code goes here - Learn more: https://docs.aws.amazon.com/cdk/latest/guide/home.html */


/* Example 1: Set up an SQS queue with an SNS topic

const queue = new sqs.Queue(this, 'sqs-queue');
const amplifyProjectInfo = AmplifyHelpers.getProjectInfo();

const sqsQueueResourceNamePrefix = `sqs-queue-${amplifyProjectInfo.projectName}`;

const queue = new sqs.Queue(this, 'sqs-queue', {
queueName: cdk.Fn.join('-', [sqsQueueResourceNamePrefix, cdk.Fn.ref('env')])
});

// 👇 create sns topic
const topic = new sns.Topic(this, 'sns-topic');

const snsTopicResourceNamePrefix = `sns-topic-${amplifyProjectInfo.projectName}`;
const topic = new sns.Topic(this, 'sns-topic', {
topicName: cdk.Fn.join('-', [snsTopicResourceNamePrefix, cdk.Fn.ref('env')])
});

// 👇 subscribe queue to topic
topic.addSubscription(new subs.SqsSubscription(queue));
Expand All @@ -34,9 +48,14 @@ export class cdkStack extends cdk.Stack {
*/

/* Example 2: Adding IAM role to the custom stack

const roleResourceNamePrefix = `CustomRole-${amplifyProjectInfo.projectName}`;

const role = new iam.Role(this, 'CustomRole', {
assumedBy: new iam.AccountRootPrincipal(),
roleName: cdk.Fn.join('-', [roleResourceNamePrefix, cdk.Fn.ref('env')])
});

*/

/* Example 3: Adding policy to the IAM role
Expand All @@ -47,5 +66,18 @@ export class cdkStack extends cdk.Stack {
}),
);
*/

/* Access other Amplify Resources

const retVal:AmplifyDependentResourcesAttributes = AmplifyHelpers.addResourceDependency(this,
amplifyResourceProps.category,
amplifyResourceProps.resourceName,
[
{category: <insert-amplify-category>, resourceName: <insert-amplify-resourcename>},
]
);

*/

}
}
3 changes: 1 addition & 2 deletions packages/amplify-category-custom/resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@types/fs-extra": "^9.0.11",
"fs-extra": "^9.1.0",
"@aws-amplify/cli-extensibility-helper": "latest",
"@aws-cdk/core": "~1.124.0",
"@aws-cdk/aws-iam": "~1.124.0",
"@aws-cdk/aws-sns-subscriptions": "~1.124.0",
Expand Down
1 change: 1 addition & 0 deletions packages/amplify-category-custom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { buildCustomResources } from './utils/build-custom-resources';
import { categoryName } from './utils/constants';
export { addCDKResourceDependency } from './utils/dependency-management-utils';
export { generateDependentResourcesType } from './utils/build-custom-resources';
export { AmplifyResourceProps } from './utils/generate-cfn-from-cdk';

export async function executeAmplifyCommand(context: $TSContext) {
let commandPath = path.normalize(path.join(__dirname, 'commands'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import * as cdk from '@aws-cdk/core';
import { JSONUtilities, pathManager } from 'amplify-cli-core';
import { categoryName } from './constants';

export type AmplifyResourceProps = {
category: string;
resourceName: string;
};

export async function generateCloudFormationFromCDK(resourceName: string) {
const targetDir = pathManager.getResourceDirectoryPath(undefined, categoryName, resourceName);
const { cdkStack } = await import(path.resolve(path.join(targetDir, 'build', 'cdk-stack.js')));

const customStack: cdk.Stack = new cdkStack(undefined, undefined, undefined, { category: categoryName, resourceName });
const amplifyResourceProps: AmplifyResourceProps = { category: categoryName, resourceName };

const customStack: cdk.Stack = new cdkStack(undefined, undefined, undefined, amplifyResourceProps);

// @ts-ignore
JSONUtilities.writeJson(path.join(targetDir, 'build', `${resourceName}-cloudformation-template.json`), customStack._toCloudFormation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ async function generateSkeletonDir(resourceName: string) {
}

const cdkFilepath = path.join(targetDir, 'cdk-stack.ts');
fs.writeFileSync(cdkFilepath, fs.readFileSync(path.join(srcResourceDirPath, 'cdk-stack.ts')));
fs.writeFileSync(cdkFilepath, fs.readFileSync(path.join(srcResourceDirPath, 'cdk-stack.ts.sample')));
}
1 change: 1 addition & 0 deletions packages/amplify-category-custom/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"references": [
{"path": "../amplify-cli-core"},
{"path": "../amplify-prompts"},
{"path": "../amplify-cli-extensibility-helper"}
]
}

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AmplifyDDBResourceTemplate } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyDDBResourceTemplate) {}
export function override(resources: AmplifyDDBResourceTemplate) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@aws-amplify/cli-extensibility-helper": "1.0.1-ext15.0"
"@aws-amplify/cli-extensibility-helper": "latest"

},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AmplifyS3ResourceTemplate } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyS3ResourceTemplate) {}
export function override(resources: AmplifyS3ResourceTemplate) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@aws-amplify/cli-extensibility-helper": "1.0.1-ext15.0"
"@aws-amplify/cli-extensibility-helper": "latest"
},
"devDependencies": {
"typescript": "^4.2.4"
Expand Down
3 changes: 2 additions & 1 deletion packages/amplify-cli-extensibility-helper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export { AmplifyAuthCognitoStackTemplate, AmplifyUserPoolGroupStackTemplate } fr
import { addCDKResourceDependency } from '@aws-amplify/amplify-category-custom';
export { AmplifyDDBResourceTemplate, AmplifyS3ResourceTemplate, AmplifyCDKL1 } from './types/storage/types';
import { getProjectInfo } from './helpers/project-info';
import { AmplifyResourceProps } from '@aws-amplify/amplify-category-custom';

export { getProjectInfo, addCDKResourceDependency };
export { getProjectInfo, addCDKResourceDependency as addResourceDependency, AmplifyResourceProps };
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AmplifyRootStackTemplate } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyRootStackTemplate) {}
export function override(resources: AmplifyRootStackTemplate) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@aws-amplify/cli-extensibility-helper": "1.0.1-ext15.0"
"@aws-amplify/cli-extensibility-helper": "latest"
},
"devDependencies": {
"typescript": "^4.2.4"
Expand Down

0 comments on commit 8bff3dc

Please sign in to comment.