AWSTemplateFormatVersion: 2010-09-09
Description: Template for deploying a static website with S3, CloudFront, ACM, and budget alerts.
Parameters:
DomainName:
Type: String
Description: The domain name for the website (optional).
CertificateArn:
Type: String
Description: The ARN of the SSL/TLS certificate in ACM.
BudgetAmount:
Type: Number
Description: The monthly budget amount in USD.
Resources:
WebsiteBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: Public-Read
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: error.html
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName: !GetAtt WebsiteBucket.DomainName
Id: S3Origin
S3OriginConfig:
OriginAccessIdentity: ''
Enabled: true
DefaultCacheBehavior:
TargetOriginId: S3Origin
ViewerProtocolPolicy: redirect-to-https
AllowedMethods:
- GET
- HEAD
CachedMethods:
- GET
- HEAD
ForwardedValues:
QueryString: false
ViewerCertificate:
AcmCertificateArn: !Ref CertificateArn
SslSupportMethod: sni-only
# Additional CloudFront configuration options can be added here (e.g., custom error responses, logging)
WebsiteBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref WebsiteBucket
PolicyDocument:
Statement:
- Sid: PublicReadGetObject
Effect: Allow
Principal: '*'
Action: s3:GetObject
Resource: !Join ['', ['arn:aws:s3:::' , !Ref WebsiteBucket, '/*']]
CostBudget:
Type: AWS::Budgets::Budget
Properties:
Budget:
BudgetType: COST
TimeUnit: MONTHLY
BudgetLimit:
Amount: !Ref BudgetAmount
Unit: USD
NotificationsWithSubscribers:
- Notifications:
- ComparisonOperator: GREATER_THAN
NotificationType: ACTUAL
Threshold: 80
ThresholdType: PERCENTAGE
Subscribers:
- SubscriptionType: EMAIL
Address: [email protected]
# Add more notification thresholds and subscribers as needed
# Optional: DNS record set for custom domain
DNSRecord:
Type: AWS::Route53::RecordSetGroup
Condition: DomainNameProvided
Properties:
HostedZoneId: your-hosted-zone-id
RecordSets:
- Name: !Ref DomainName
Type: A
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2
DNSName: !GetAtt CloudFrontDistribution.DomainName
Conditions:
DomainNameProvided: !Not [!Equals [!Ref DomainName, '']]
Outputs:
WebsiteURL:
Value: !GetAtt CloudFrontDistribution.DomainName
S3BucketName:
Value: !Ref WebsiteBucket
Deployment Steps:
-
Customize the Template:
- Replace placeholders like
[email protected]
and your-hosted-zone-id
with your actual values.
- Provide the ARN of your ACM certificate for
CertificateArn
.
- Set the desired budget amount in USD for
BudgetAmount
.
- If using a custom domain, fill in
DomainName
.
- Adjust CloudFront configuration options as needed.
-
Save the Template: Save the template as a YAML or JSON file (e.g., website.yaml
).
-
Deploy using AWS CloudFormation:
- Use the AWS Management Console, AWS CLI, or SDKs to create a CloudFormation stack using the template file.
-
Upload Website Content: Upload your website files (HTML, CSS, JavaScript, images) to the created S3 bucket.
-
Test and Monitor: Access your website using the CloudFront URL and verify its functionality. Monitor your costs and resource usage.
Additional Notes:
- Security: Ensure your S3 bucket has appropriate access controls (e.g., bucket policies) to restrict unauthorized access.
- Caching: Adjust CloudFront caching behavior settings to optimize performance for your specific content.
- Cost Optimization: Regularly review your AWS costs and adjust budget thresholds as needed.
- Route 53: If not using a custom domain, you can access your website using the CloudFront distribution's domain name.
By following these steps and customizing the template to your requirements, you can deploy a secure, performant, and cost-effective static website on AWS.