Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
addS3(bucketName: string, role?: IAM.IPrincipal, keepBucket = true) {
const removalPolicy = keepBucket ? RemPolicy.Orphan : RemPolicy.Destroy
const s3Props = { bucketName, removalPolicy }
const bucket = new S3.Bucket(this, `${this.id}-${bucketName}`, s3Props)
// tslint:disable-next-line:no-unused-expression
role && bucket.grantReadWrite(role)
return bucket
}
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`, {
constructor(parent, id, props) {
super(parent, id, props);
// Create a network for the application to run in
this.vpc = new ec2.VpcNetwork(this, 'vpc', {
maxAZs: 2,
natGateways: 1
});
// Create an ECS cluster
this.cluster = new ecs.Cluster(this, 'cluster', {
vpc: this.vpc
});
// Create S3 bucket
this.screenshotBucket = new s3.Bucket(this, 'screenshot-bucket', {
publicReadAccess: true
});
// Create queue
this.screenshotQueue = new sqs.Queue(this, 'screenshot-queue');
// Create DynamoDB table
this.screenshotTable = new dynamodb.Table(this, 'screenshots', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.String },
billingMode: dynamodb.BillingMode.PayPerRequest
});
}
}
let encryptionKey: kms.IKey | undefined;
if (encryption === TableEncryption.CLIENT_SIDE_KMS && props.encryptionKey === undefined) {
// CSE-KMS should behave the same as SSE-KMS - use the provided key or create one automatically
// Since Bucket only knows about SSE, we repeat the logic for CSE-KMS at the Table level.
encryptionKey = new kms.Key(table, 'Key');
} else {
encryptionKey = props.encryptionKey;
}
// create the bucket if none was provided
if (!bucket) {
if (encryption === TableEncryption.CLIENT_SIDE_KMS) {
bucket = new s3.Bucket(table, 'Bucket');
} else {
bucket = new s3.Bucket(table, 'Bucket', {
encryption: encryptionMappings[encryption],
encryptionKey
});
encryptionKey = bucket.encryptionKey;
}
}
return {
bucket,
encryption,
encryptionKey
};
}
throw new Error('you can not specify encryption settings if you also provide a bucket');
}
let encryptionKey: kms.IKey | undefined;
if (encryption === TableEncryption.CLIENT_SIDE_KMS && props.encryptionKey === undefined) {
// CSE-KMS should behave the same as SSE-KMS - use the provided key or create one automatically
// Since Bucket only knows about SSE, we repeat the logic for CSE-KMS at the Table level.
encryptionKey = new kms.Key(table, 'Key');
} else {
encryptionKey = props.encryptionKey;
}
// create the bucket if none was provided
if (!bucket) {
if (encryption === TableEncryption.CLIENT_SIDE_KMS) {
bucket = new s3.Bucket(table, 'Bucket');
} else {
bucket = new s3.Bucket(table, 'Bucket', {
encryption: encryptionMappings[encryption],
encryptionKey
});
encryptionKey = bucket.encryptionKey;
}
}
return {
bucket,
encryption,
encryptionKey
};
}
private addS3() {
const s3Props = { bucketName: this.s3Name }
const bucket = new S3.Bucket(this, `${this.appName}-item-bucket`, s3Props)
bucket.grantReadWrite(this.role)
}
constructor(scope: Construct, id: string, props: WebsiteBucketProps = {}) {
super(scope, id);
const {
bucketName,
removalPolicy = RemovalPolicy.RETAIN,
disableUpload = false,
source,
websiteIndexDocument,
websiteErrorDocument,
} = props;
const bucket = new Bucket(this, 'WebsiteBucket', {
bucketName,
removalPolicy,
websiteIndexDocument: websiteIndexDocument || 'index.html',
websiteErrorDocument: websiteErrorDocument || 'error.html',
});
const originId = new CfnCloudFrontOriginAccessIdentity(
this,
'OriginAccessIdentity',
{
cloudFrontOriginAccessIdentityConfig: {
comment: `CloudFront OriginAccessIdentity for ${bucket.bucketName}`,
},
},
);
constructor(scope) {
super(scope, 'scale-your-cloudformation');
const domainName = 'scaleyourcloudformation.com';
const cert = new Certificate(this, 'cert', {
domainName,
validationMethod: ValidationMethod.DNS,
});
const websiteBucket = new Bucket(this, 'WebsiteBucket', {
domainName,
removalPolicy: RemovalPolicy.DESTROY,
websiteIndexDocument: 'index.html',
websiteErrorDocument: 'error.html',
});
const originId = new CfnCloudFrontOriginAccessIdentity(
this,
'OriginAccessIdentity',
{
cloudFrontOriginAccessIdentityConfig: {
comment: `CloudFront OriginAccessIdentity for ${websiteBucket.bucketName}`,
},
},
);
constructor(scope: cdk.Construct, id: string) {
super(scope, id);
const encryptionKey = new kms.Key(this, 'CrossRegionCodePipelineReplicationBucketEncryptionKey', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
const encryptionAlias = new AliasWithShorterGeneratedName(this, 'CrossRegionCodePipelineReplicationBucketEncryptionAlias', {
targetKey: encryptionKey,
aliasName: cdk.PhysicalName.GENERATE_IF_NEEDED,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
this.replicationBucket = new s3.Bucket(this, 'CrossRegionCodePipelineReplicationBucket', {
bucketName: cdk.PhysicalName.GENERATE_IF_NEEDED,
encryptionKey: encryptionAlias,
});
}
}