/** * BIMI logo + PEM hosting: private S3 + CloudFront OAC * CDK TypeScript — AWS CDK v2 * * npx cdk deploy -c domainName=bimi.example.com -c hostedZoneId=ZXXXX */ import * as cdk from 'aws-cdk-lib'; import * as acm from 'aws-cdk-lib/aws-certificatemanager'; import * as cloudfront from 'aws-cdk-lib/aws-cloudfront'; import * as origins from 'aws-cdk-lib/aws-cloudfront-origins'; import * as route53 from 'aws-cdk-lib/aws-route53'; import * as targets from 'aws-cdk-lib/aws-route53-targets'; import * as s3 from 'aws-cdk-lib/aws-s3'; import { Construct } from 'constructs'; export class BimiLogoHostingStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); const domainName = this.node.tryGetContext('domainName') as string; const hostedZoneId = this.node.tryGetContext('hostedZoneId') as string; if (!domainName || !hostedZoneId) { throw new Error('Pass -c domainName=... -c hostedZoneId=...'); } const zone = route53.HostedZone.fromHostedZoneAttributes(this, 'Zone', { hostedZoneId, zoneName: domainName.split('.').slice(-2).join('.'), }); const bucket = new s3.Bucket(this, 'BimiBucket', { blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL, encryption: s3.BucketEncryption.S3_MANAGED, enforceSSL: true, removalPolicy: cdk.RemovalPolicy.RETAIN, }); const certificate = new acm.DnsValidatedCertificate(this, 'Cert', { domainName, hostedZone: zone, region: 'us-east-1', }); const distribution = new cloudfront.Distribution(this, 'Distribution', { defaultBehavior: { origin: origins.S3BucketOrigin.withOriginAccessControl(bucket), viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS, compress: true, cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD, }, domainNames: [domainName], certificate, priceClass: cloudfront.PriceClass.PRICE_CLASS_100, minimumProtocolVersion: cloudfront.SecurityPolicyProtocol.TLS_V1_2_2021, comment: 'BIMI logo and mark certificate', }); new route53.ARecord(this, 'Alias', { zone, recordName: domainName, target: route53.RecordTarget.fromAlias(new targets.CloudFrontTarget(distribution)), }); new cdk.CfnOutput(this, 'BucketName', { value: bucket.bucketName }); new cdk.CfnOutput(this, 'DistributionId', { value: distribution.distributionId }); new cdk.CfnOutput(this, 'LogoUrl', { value: `https://${domainName}/logo.svg` }); new cdk.CfnOutput(this, 'PemUrl', { value: `https://${domainName}/certificate.pem` }); } }