Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async createStack(template: any, name: string, defParams: any = {}, addAppSyncApiName: boolean = true) {
const params = [];
if (addAppSyncApiName === true) {
params.push({
ParameterKey: ResourceConstants.PARAMETERS.AppSyncApiName,
ParameterValue: name,
});
}
for (const key of Object.keys(defParams)) {
params.push({
ParameterKey: key,
ParameterValue: defParams[key],
});
}
// add env info to template
template.Parameters.env = {
Type: 'String',
Description: 'env name',
Default: 'NONE',
const parametersForStack = Object.keys(userDefinedStack.Parameters).reduce(
(acc, k) => ({
...acc,
[k]: customStackParams[k],
}),
{}
);
transformStacks[userStack] = userDefinedStack;
// Split on non alphabetic characters to make a valid resource id.
const stackResourceId = userStack.split(/[^A-Za-z]/).join('');
const customNestedStack = new CloudFormation.Stack({
Parameters: parametersForStack,
TemplateURL: Fn.Join('/', [
'https://s3.amazonaws.com',
Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentBucket),
Fn.Ref(ResourceConstants.PARAMETERS.S3DeploymentRootKey),
'stacks',
userStack,
]),
}).dependsOn(allResourceIds);
rootStack.Resources[stackResourceId] = customNestedStack;
}
// Update the Root Stack Params since we have added the Child Stack Params if they are missing.
rootStack.Parameters = updatedParameters;
return {
...transformOutput,
resolvers: transformResolvers,
stacks: transformStacks,
};
}
public makeParams() {
return {
[ResourceConstants.PARAMETERS.AuthCognitoUserPoolId]: new StringParameter({
Description: 'The id of an existing User Pool to connect. If this is changed, a user pool will not be created for you.',
Default: ResourceConstants.NONE
})
}
}
public format(ctx: TransformerContext): DeploymentResources {
ctx.mergeConditions(this.schemaResourceUtil.makeEnvironmentConditions());
const resolversFunctionsAndSchema = this.collectResolversFunctionsAndSchema(ctx);
const defaultDependencies = [ResourceConstants.RESOURCES.GraphQLSchemaLogicalID];
if (ctx.getResource(ResourceConstants.RESOURCES.NoneDataSource)) {
defaultDependencies.push(ResourceConstants.RESOURCES.NoneDataSource);
}
const nestedStacks = splitStack({
stack: ctx.template,
stackRules: ctx.getStackMapping(),
defaultParameterValues: {
[ResourceConstants.PARAMETERS.AppSyncApiId]: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
},
defaultParameterDefinitions: {
[ResourceConstants.PARAMETERS.AppSyncApiId]: new StringParameter({
Description: `The id of the AppSync API associated with this project.`,
}),
},
deployment: {
deploymentBucketParameterName: ResourceConstants.PARAMETERS.S3DeploymentBucket,
deploymentKeyParameterName: ResourceConstants.PARAMETERS.S3DeploymentRootKey,
},
importExportPrefix: Fn.Ref(ResourceConstants.PARAMETERS.AppSyncApiId),
defaultDependencies,
});
return {
...nestedStacks,
...resolversFunctionsAndSchema,
export function syncLambdaArnResource({ name, region }: { name: string; region?: string }) {
const env = 'env;';
const substitutions = {};
if (referencesEnv(name)) {
substitutions[env] = Fn.Ref(ResourceConstants.PARAMETERS.Env);
}
return Fn.If(
ResourceConstants.CONDITIONS.HasEnvironmentParameter,
Fn.Sub(lambdaArnKey(name, region), substitutions),
Fn.Sub(lambdaArnKey(removeEnvReference(name), region), {})
);
}
export function lambdaArnKey(name: string, region?: string) {
function joinWithEnv(separator: string, listToJoin: any[]) {
return Fn.If(
ResourceConstants.CONDITIONS.HasEnvironmentParameter,
Fn.Join(separator, [...listToJoin, Fn.Ref(ResourceConstants.PARAMETERS.Env)]),
Fn.Join(separator, listToJoin)
);
}
export function syncLambdaIAMRole({ name, region }: { name: string; region?: string }) {
export function lambdaArnResource(name: string, region?: string) {
const substitutions = {};
if (referencesEnv(name)) {
substitutions['env'] = Fn.Ref(ResourceConstants.PARAMETERS.Env);
}
return Fn.If(
ResourceConstants.CONDITIONS.HasEnvironmentParameter,
Fn.Sub(lambdaArnKey(name, region), substitutions),
Fn.Sub(lambdaArnKey(removeEnvReference(name), region), {})
);
}
datasource = (name: string, region: string): any => {
return new AppSync.DataSource({
ApiId: Fn.Ref(ResourceConstants.PARAMETERS.AppSyncApiId),
Name: FunctionResourceIDs.FunctionDataSourceID(name, region),
Type: 'AWS_LAMBDA',
ServiceRoleArn: Fn.GetAtt(FunctionResourceIDs.FunctionIAMRoleID(name, region), 'Arn'),
LambdaConfig: {
LambdaFunctionArn: lambdaArnResource(name, region),
},
}).dependsOn(FunctionResourceIDs.FunctionIAMRoleID(name, region));
};
private replaceEnv(value: string): Value {
if (!this.referencesEnv(value)) {
return value;
}
return Fn.Sub(value, {
env: Fn.Ref(ResourceConstants.PARAMETERS.Env),
});
}
public makeResolverS3RootParams(): Template {
return {
Parameters: {
[ResourceConstants.PARAMETERS.Env]: new StringParameter({
Description: `The environment name. e.g. Dev, Test, or Production`,
Default: ResourceConstants.NONE,
}),
[ResourceConstants.PARAMETERS.S3DeploymentBucket]: new StringParameter({
Description: 'The S3 bucket containing all deployment assets for the project.',
}),
[ResourceConstants.PARAMETERS.S3DeploymentRootKey]: new StringParameter({
Description: 'An S3 key relative to the S3DeploymentBucket that points to the root of the deployment directory.',
}),
},
};
}