Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Last active April 22, 2024 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhavaln/94e5812c3dbb7fb11198cbdec4a1faa6 to your computer and use it in GitHub Desktop.
Save dhavaln/94e5812c3dbb7fb11198cbdec4a1faa6 to your computer and use it in GitHub Desktop.
AWS Script Helper with Google Gemini Pro 1.5 and CrewAI

Requirements

I need to host a simple website to list out current services of my company and also share contact information. Please make sure to enable HTTPS and caching on the website. The website will be accessible from anywhere in the world. I would also like to configure some kind of cost alert to ensure that the overall spending is within a limit.

Agents

  • AWS Infrastructure and DevOps Specialist
  • AWS CloudFormation Engineer

Tools

  • Web Search
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: your-email@example.com
        # 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:

  1. Customize the Template:

    • Replace placeholders like your-email@example.com 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.
  2. Save the Template: Save the template as a YAML or JSON file (e.g., website.yaml).

  3. Deploy using AWS CloudFormation:

    • Use the AWS Management Console, AWS CLI, or SDKs to create a CloudFormation stack using the template file.
  4. Upload Website Content: Upload your website files (HTML, CSS, JavaScript, images) to the created S3 bucket.

  5. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment