Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function buildMyStack(scope: cdk.Construct, id: string, props: {}) {
const stack = new cdk.Stack(scope, id, props);
// TODO: inject environment variables
// should be via SSM. See here:
// https://docs.aws.amazon.com/cdk/latest/guide/get_ssm_value.html
await execute("./node_modules/.bin/next", ["build"]);
const manifest = await getBuildManifest();
const staticPages = Object.values(manifest.pages.html);
const bucket = new s3.Bucket(scope, `${id}-bucket`, {
publicReadAccess: true
});
new s3Upload.BucketDeployment(stack, `${id}-s3-static-pages`, {
// TODO: these files might already be prefixed with `pages/${page}`? This will need to be removed if so.
sources: staticPages.map(page =>
s3Upload.Source.asset(`./.next/serverless/${page}`)
),
destinationBucket: bucket,
destinationKeyPrefix: "static-pages"
});
new s3Upload.BucketDeployment(stack, `${id}-s3-next-static-files`, {
sources: [s3Upload.Source.asset("./.next/static")],
destinationBucket: bucket,
destinationKeyPrefix: "_next/static"
});
new s3Upload.BucketDeployment(stack, `${id}-s3-public-files`, {
sources: [s3Upload.Source.asset("./public")],
destinationBucket: bucket,
destinationKeyPrefix: "public"
const manifest = await getBuildManifest();
const staticPages = Object.values(manifest.pages.html);
const bucket = new s3.Bucket(scope, `${id}-bucket`, {
publicReadAccess: true
});
new s3Upload.BucketDeployment(stack, `${id}-s3-static-pages`, {
// TODO: these files might already be prefixed with `pages/${page}`? This will need to be removed if so.
sources: staticPages.map(page =>
s3Upload.Source.asset(`./.next/serverless/${page}`)
),
destinationBucket: bucket,
destinationKeyPrefix: "static-pages"
});
new s3Upload.BucketDeployment(stack, `${id}-s3-next-static-files`, {
sources: [s3Upload.Source.asset("./.next/static")],
destinationBucket: bucket,
destinationKeyPrefix: "_next/static"
});
new s3Upload.BucketDeployment(stack, `${id}-s3-public-files`, {
sources: [s3Upload.Source.asset("./public")],
destinationBucket: bucket,
destinationKeyPrefix: "public"
});
new s3Upload.BucketDeployment(stack, `${id}-s3-static-files`, {
sources: [s3Upload.Source.asset("./static")],
destinationBucket: bucket,
destinationKeyPrefix: "static"
});
const ssrLambda = new lambda.Function(stack, `${id}-ssr-lambda`, {
constructor(parent, id, props) {
super(parent, id, props);
// Copy the static files into the static S3 bucket
this.s3Deployment = new s3Deployment.BucketDeployment(this, 'deploy-web', {
source: s3Deployment.Source.asset('./static'),
destinationBucket: props.staticBucket,
});
// Create a lambda that regenerates the homepage
const regenerateHomepage = new lambda.Function(this, 'regenerate-homepage', {
runtime: lambda.Runtime.NODEJS_10_X,
handler: 'regenerate-homepage.handle',
code: lambda.Code.asset('./app/regenerate-homepage'),
environment: {
CHANGELOGS_TABLE_NAME: props.changelogsTable.tableName,
FEEDS_TABLE_NAME: props.feedsTable.tableName,
WEB_BUCKET_NAME: props.webBucket.bucketName
}
});
constructor(parent, id, props) {
super(parent, id, props);
// Copy the static files into the static S3 bucket
this.s3Deployment = new s3Deployment.BucketDeployment(this, 'deploy-web', {
source: s3Deployment.Source.asset('./static'),
destinationBucket: props.staticBucket,
});
// Create a lambda that regenerates the homepage
const regenerateHomepage = new lambda.Function(this, 'regenerate-homepage', {
runtime: lambda.Runtime.NODEJS_10_X,
handler: 'regenerate-homepage.handle',
code: lambda.Code.asset('./app/regenerate-homepage'),
environment: {
CHANGELOGS_TABLE_NAME: props.changelogsTable.tableName,
FEEDS_TABLE_NAME: props.feedsTable.tableName,
WEB_BUCKET_NAME: props.webBucket.bucketName
}
});
// Grant the lambda permission to read the tables
'OriginAccessIdentity',
{
cloudFrontOriginAccessIdentityConfig: {
comment: `CloudFront OriginAccessIdentity for ${bucket.bucketName}`,
},
},
);
bucket.grantRead(
new CanonicalUserPrincipal(originId.attrS3CanonicalUserId),
);
if (!disableUpload) {
const placeHolderSource = path.join(__dirname, '..', 'website');
new BucketDeployment(this, 'WebsiteDeployment', {
sources: [Source.asset(source || placeHolderSource)],
destinationBucket: bucket,
retainOnDelete: removalPolicy === RemovalPolicy.RETAIN,
});
}
this.s3OriginConfig = {
originAccessIdentityId: originId.ref,
s3BucketSource: bucket,
};
}
}
},
],
aliasConfiguration: {
acmCertRef: cert.certificateArn,
names: [domainName],
},
};
let distribution = new CloudFrontWebDistribution(
this,
'WebSiteDistribution',
distributionConfig,
);
const placeHolderSource = path.join(__dirname, '..', 'docs');
new BucketDeployment(this, 'WebsiteDeployment', {
sources: [Source.asset(placeHolderSource)],
destinationBucket: websiteBucket,
distribution,
retainOnDelete: false,
});
new PipelineConstruct(this);
}
}
sources: staticPages.map(page =>
s3Upload.Source.asset(`./.next/serverless/${page}`)
),
{
cloudFrontOriginAccessIdentityConfig: {
comment: `CloudFront OriginAccessIdentity for ${bucket.bucketName}`,
},
},
);
bucket.grantRead(
new CanonicalUserPrincipal(originId.attrS3CanonicalUserId),
);
if (!disableUpload) {
const placeHolderSource = path.join(__dirname, '..', 'website');
new BucketDeployment(this, 'WebsiteDeployment', {
sources: [Source.asset(source || placeHolderSource)],
destinationBucket: bucket,
retainOnDelete: removalPolicy === RemovalPolicy.RETAIN,
});
}
this.s3OriginConfig = {
originAccessIdentityId: originId.ref,
s3BucketSource: bucket,
};
}
}
],
aliasConfiguration: {
acmCertRef: cert.certificateArn,
names: [domainName],
},
};
let distribution = new CloudFrontWebDistribution(
this,
'WebSiteDistribution',
distributionConfig,
);
const placeHolderSource = path.join(__dirname, '..', 'docs');
new BucketDeployment(this, 'WebsiteDeployment', {
sources: [Source.asset(placeHolderSource)],
destinationBucket: websiteBucket,
distribution,
retainOnDelete: false,
});
new PipelineConstruct(this);
}
}