cloud computing aws

AWS Core Services Deep Dive

In this chapter, we'll explore the core AWS services that form the foundation of cloud computing.

Amazon EC2 (Elastic Compute Cloud)

Instance Types and Selection

# List available instance types
aws ec2 describe-instance-types

# Launch an EC2 instance with user data
aws ec2 run-instances \
    --image-id ami-0c55b159cbfafe1f0 \
    --instance-type t2.micro \
    --user-data file://startup-script.sh

Auto Scaling

# Auto Scaling Group configuration
Resources:
  WebServerGroup:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      MinSize: '1'
      MaxSize: '3'
      DesiredCapacity: '2'
      LaunchTemplate:
        LaunchTemplateId: !Ref WebServerLaunchTemplate
        Version: !GetAtt WebServerLaunchTemplate.LatestVersionNumber

Amazon S3 (Simple Storage Service)

Bucket Operations

import boto3

s3 = boto3.client('s3')

# Create bucket
s3.create_bucket(
    Bucket='my-unique-bucket-name',
    CreateBucketConfiguration={
        'LocationConstraint': 'us-west-2'
    }
)

# Upload file with encryption
s3.put_object(
    Bucket='my-unique-bucket-name',
    Key='file.txt',
    Body=open('file.txt', 'rb'),
    ServerSideEncryption='AES256'
)

Static Website Hosting

# Enable static website hosting
aws s3 website s3://my-bucket/ \
    --index-document index.html \
    --error-document error.html

# Set bucket policy for public access
aws s3api put-bucket-policy \
    --bucket my-bucket \
    --policy file://policy.json

Amazon RDS (Relational Database Service)

Database Setup

# Create RDS instance
aws rds create-db-instance \
    --db-instance-identifier mydb \
    --db-instance-class db.t3.micro \
    --engine mysql \
    --master-username admin \
    --master-user-password mypassword \
    --allocated-storage 20

Backup and Restore

import boto3

rds = boto3.client('rds')

# Create snapshot
response = rds.create_db_snapshot(
    DBSnapshotIdentifier='mydb-snapshot',
    DBInstanceIdentifier='mydb'
)

# Restore from snapshot
response = rds.restore_db_instance_from_db_snapshot(
    DBInstanceIdentifier='mydb-restored',
    DBSnapshotIdentifier='mydb-snapshot'
)

AWS Lambda

Function Creation

# Lambda function example
def lambda_handler(event, context):
    # Process S3 event
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']
    
    # Process the file
    process_file(bucket, key)
    
    return {
        'statusCode': 200,
        'body': f'Processed {key} from {bucket}'
    }

API Gateway Integration

# Serverless framework configuration
functions:
  api:
    handler: handler.lambda_handler
    events:
      - http:
          path: /process
          method: post

Amazon VPC (Virtual Private Cloud)

Network Configuration

# Create VPC
aws ec2 create-vpc --cidr-block 10.0.0.0/16

# Create subnet
aws ec2 create-subnet \
    --vpc-id vpc-123456 \
    --cidr-block 10.0.1.0/24

# Create internet gateway
aws ec2 create-internet-gateway

Security Groups

import boto3

ec2 = boto3.client('ec2')

# Create security group
response = ec2.create_security_group(
    GroupName='WebServerSG',
    Description='Web Server Security Group'
)

# Add inbound rules
ec2.authorize_security_group_ingress(
    GroupId=response['GroupId'],
    IpPermissions=[
        {
            'IpProtocol': 'tcp',
            'FromPort': 80,
            'ToPort': 80,
            'IpRanges': [{'CidrIp': '0.0.0.0/0'}]
        }
    ]
)

Amazon CloudFront

CDN Setup

# Create distribution
aws cloudfront create-distribution \
    --origin-domain-name my-bucket.s3.amazonaws.com \
    --default-root-object index.html

Cache Configuration

{
  "DistributionConfig": {
    "DefaultCacheBehavior": {
      "MinTTL": 0,
      "DefaultTTL": 86400,
      "MaxTTL": 31536000,
      "ForwardedValues": {
        "QueryString": false,
        "Cookies": {
          "Forward": "none"
        }
      }
    }
  }
}

Hands-on Exercise: Building a Scalable Web Application

  1. Set up VPC and Networking
# Create VPC
VPC_ID=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query 'Vpc.VpcId' --output text)

# Create public subnet
SUBNET_ID=$(aws ec2 create-subnet \
    --vpc-id $VPC_ID \
    --cidr-block 10.0.1.0/24 \
    --query 'Subnet.SubnetId' \
    --output text)
  1. Launch Application Servers
# Launch EC2 instances
aws ec2 run-instances \
    --image-id ami-0c55b159cbfafe1f0 \
    --instance-type t2.micro \
    --subnet-id $SUBNET_ID \
    --count 2
  1. Set up Database
# Create RDS instance
aws rds create-db-instance \
    --db-instance-identifier myapp-db \
    --engine mysql \
    --db-instance-class db.t3.micro
  1. Configure Load Balancer
# Create application load balancer
aws elbv2 create-load-balancer \
    --name myapp-lb \
    --subnets $SUBNET_ID \
    --security-groups $SG_ID

In the next chapter, we'll explore advanced AWS services and architectural patterns.