- Create/confirm a Vite app builds successfully and produces
dist/output (Vite's default build output directory isdist). - Add a
buildspec.ymlto the repo root so CodeBuild knows how to build and what to publish; useartifacts.base-directoryto point atdist. - Add a visible "version" string (or change text) in the UI so each commit can be verified on the live site.
- In Terraform, create an
aws_codestarconnections_connection(CodeConnections) for GitHub. - After the resource exists, complete the required one-time authorization/installation step in AWS so the connection becomes active for CodePipeline use.
- Create two private buckets:
artifact_bucket: For CodePipeline artifacts (private).website_bucket: For website content (fully private — no static website hosting, no public access). signiance
- Do NOT enable static website hosting on the website bucket (CloudFront will serve the content instead).
- Create IAM roles:
- CodePipeline role: Access to artifact bucket, permission to use the GitHub connection, permission to run CodeBuild, and permission to deploy to the website bucket.
- CodeBuild role: Permission for logs + read/write artifacts.
- Create an
aws_cloudfront_origin_access_controlresource:signing_behavior = "always"signing_protocol = "sigv4"(modern, short-lived credentials). aws.amazon
- Create an
aws_cloudfront_distributionpointing to the private S3 website bucket:- Configure the OAC as the origin identity.
- Set cache behaviors with appropriate TTLs.
- Enable HTTPS and HTTP/2.
- Create an
aws_s3_bucket_policythat:- Restricts
s3:GetObjectto CloudFront only. - Uses condition
aws:SourceArnmatching the distribution ARN. dev
- Restricts
- Block public access on the website bucket (all four options set to
true). signiance
- Create an
aws_codebuild_projectthat uses source typeCODEPIPELINEand readsbuildspec.ymlfrom the source artifact (repo). - Ensure the build artifact output is the Vite build directory by setting
artifactsin the buildspec withbase-directory: distandfiles: '**/*'.
- Create an
aws_codepipelinewith 4 stages (updated from 3):- Source stage (GitHub): Use
CodeStarSourceConnectionwithConnectionArn,FullRepositoryId(owner/repo, case-sensitive), andBranchName. - Build stage (CodeBuild): Input is
SourceOutput, output isBuildOutput. - Deploy stage (S3): Use S3 deploy action configured with
BucketNameandExtract=trueto upload files to the private bucket. - NEW — Invalidation stage (CloudFront): Use CloudFront invalidate cache action to clear the distribution cache (
/*path) after deployment. youtube
- Source stage (GitHub): Use
- Run
terraform apply, then commit a change to the configured branch and confirm the pipeline triggers. - Confirm:
- CodeBuild produces a build artifact.
- Deploy stage uploads files to the private S3 bucket.
- CloudFront invalidation clears cache.
- Direct S3 URL access returns
403 Forbidden. youtube - CloudFront domain URL (e.g.,
d1234.cloudfront.net) loads the React app securely via HTTPS. - The visible version/text change appears on the CloudFront domain (not S3 URL).