We've created a stack that we've called s3Stack. This stack inherits from the class Stack that represents a single CloudFormation Stack. In our s3Stack constructor we create a new s3 bucket with public read access and we've specified for AWS that this bucket will be used to host a website by indicating the websiteIndexDocument parameter. To specify the sources of our S3 bucket we used BucketDeployment class which is in aws-cdk-lib/aws-s3-deployment module. CfnOutput give us the ability to write some results of our deployment execution to CloudFormation output and also in the output of cdk deploy --all command.
Chapter VI: Adding CloudFront
Let's modify our code now to include a CloudFront distribution and fix policies on our S3 bucket.
What we've done here is to change the permissions on the S3 bucket; disabling publicReadAccess and blocking all public access. We then create a CloudFront Origin Access Identity and give it permission to access objects on our S3 bucket. Finally we create a CloudFront distribution parameters to have as an origin our S3 bucket with restricted HTTP methods to GET HEAD and OPTIONS.
To deploy this new version of our infrastructue you need to add context variables (accountId and region) to cdk.json file and then execute this command:
Chapter VII: Adding our own domain name
You can register a new domain name or migrate your domain name to AWS Route 53. It is pretty straight forward. Once that is done, we can configure Route 53 now to redirect traffic to our CloudFront distribution. We can also create TLS certificate for our website.
To deploy the final version of our infrastructue you need to add two more context variables (subdomain and domain) to cdk.json file and then execute the same command as always.