Files
oam/knowledge base/cloud computing/aws/ec2.md

12 KiB

Elastic Compute Cloud

  1. TL;DR
  2. Burstable instances
  3. Disks
  4. Metrics
  5. Auto scaling
    1. Lifecycle hooks
  6. Image builder
  7. Further readings
    1. Sources

TL;DR

EC2 instances are billed by the second, with a minimum of 60s, since 2017-10-02.

Use an instance profile to allow an EC2 instance to use an IAM role.

T instances launch as unlimited by default. Launch them in standard mode to avoid paying for surplus credits.

The instance type can be changed. The procedure depends on the root volume, but does require downtime.

Clone EC2 instances by:

  1. Creating an AMI from the original instance. Mind the default behaviour of the AMI creator is to shutdown the instance, take a snapshot, and boot it again to guarantee the image's filesystem integrity.
  2. Using that AMI to launch clones identical to the original.

Consider using specialized AMIs for specialized purposes.
E.g., using AL2023 based Amazon ECS AMIs to host containerized workloads.

Real world use cases
# Get the IDs of running nginx instances in 'dev'.
aws ec2 describe-instances --output 'text' \
  --query 'Reservations[].Instances[].InstanceId[]'
  --filters \
    'Name=instance-state-name,Values=running' \
    'Name=tag:env,Values=dev' \
    'Name=tag:app,Values=nginx' \

# Start SSM sessions to specific machines.
aws ec2 describe-instances --output text \
  --query 'Reservations[].Instances[].InstanceId' \
  --filters \
    'Name=app,Values=mysql' \
    'Name=instance-state-name,Values=running' \
| xargs -ot aws ssm start-session --target

# Show images details.
aws ec2 describe-images --image-ids 'ami-8b8c57f8'
aws ec2 describe-images --filters \
  'Name=name,Values=["al2023-ami-minimal-*"]' \
  'Name=owner-alias,Values=["amazon"]' \
  'Name=architecture,Values=["arm64","x86_64"]' \
  'Name=block-device-mapping.volume-type,Values=["gp3"]'

# Describe security groups.
aws ec2 describe-security-groups --group-names 'pulumi-workshop'

# Delete security groups.
aws ec2 delete-security-group --group-name 'pulumi-workshop'
aws ec2 delete-security-group --group-id 'sg-0773aa724d0c2dd51'

# Query the onboard IMDSv1 metadata server.
curl 'http://instance-data/latest/meta-data/instance-id'
curl 'http://169.254.169.254/latest/meta-data/instance-type'
curl 'http://[fd00:ec2::254]/latest/meta-data/local-ipv4'

# Configure the CloudWatch agent
amazon-cloudwatch-agent-ctl -a 'status'
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a 'set-log-level' -l 'INFO'
amazon-cloudwatch-agent-ctl -a 'fetch-config' -m 'ec2' -s -c 'file:/opt/custom/aws/cloudwatch/agent-config.json'
tail -f '/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log'

Burstable instances

T instances are burstable.

Refer Burstable performance instances and Key concepts and definitions for burstable performance instances.

Traditional EC2 instance types provide fixed CPU resources.
Burstable performance instances provide a baseline level of CPU utilization, with the ability to burst CPU utilization above the baseline level.

One only pays for the baseline CPU, plus any additional burst CPU usage over a 24-hour period.

The baseline utilization and ability to burst are governed by CPU credits.
Burstable performance instances continuously earn credits when they stays below the CPU baseline, and continuously spend credits when they bursts above the baseline.
Accrued credits can be used later to burst above baseline CPU utilization.
Credits can be accrued only up to a point. How high this limit is depends on the instance type.
When the credits spent are more than credits earned, the instance behavior depends on the credit configuration mode (Standard or Unlimited).

In Standard mode, burstable instances:

  • Use the accrued credits to burst above baseline CPU utilization when they are available.
  • Gradually come down to baseline CPU utilization if there are no accrued credits remaining.
  • Cannot burst above baseline until they accrue more credits.

In Unlimited mode, burstable instances:

  • Use the accrued credits to burst above baseline CPU utilization when they are available.
  • Spend surplus credits to continue bursting above baseline if there are no accrued credits remaining.
  • Use CPU credits they earn to pay down the surplus credits they spent earlier when CPU utilization falls below the baseline again.

Earning CPU credits to pay down surplus credits enables EC2 to average the CPU utilization of instances over a 24-hour period.
If the average CPU usage over a 24-hour period exceeds the baseline, instances are billed for the additional usage.

Disks

Refer EBS and Device names for volumes on Amazon EC2 instances.

Metrics

Instances publish a default set of metrics to CloudWatch with no charge.
One can change this set by configuring the CloudWatch agent.

Refer How can I send memory and disk metrics from my EC2 instances to CloudWatch?.

Make sure the instance the permissions it needs to publish extra metrics.
Consider assigning it the AWS-managed CloudWatchAgentServerPolicy IAM policy or similar permissions.

CloudWatch agent's logs are saved by default to /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log.

amazon-cloudwatch-agent-ctl -a 'status'
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a 'set-log-level' -l 'INFO'
amazon-cloudwatch-agent-ctl -a 'fetch-config' -m 'ec2' -s -c 'file:/opt/aws/amazon-cloudwatch-agent/bin/config.json'
tail -f '/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log'

Auto scaling

Refer Amazon EC2 Auto Scaling.

Lifecycle hooks

Refer Amazon EC2 Auto Scaling lifecycle hooks.

Also see CompleteLifecycleAction.

Image builder

Refer EC2 Image Builder.

AWS service automating the creation, management, and deployment of customized AMIs or Docker images.

AMIs created by Image Builder in one's account are owned by that account.

Image Builder supports the following at the time of writing:

Operating system/distribution Supported versions
Amazon Linux 2, 2023
CentOS 7, 8
CentOS Stream 8
Mac OS X 12.x (Monterey), 13.x (Ventura), 14.x (Sonoma)
Red Hat Enterprise Linux (RHEL) 7, 8, 9
SUSE Linux Enterprise Server (SLE) 12, 15
Ubuntu 18.04 LTS, 20.04 LTS, 22.04 LTS, 24.04 LTS
Windows Server 2012 R2, 2016, 2019, 2022

Image Builder costs nothing to create custom AMI or container images.
However, standard pricing applies for other services that are used in the process.

Steps:

AMI creation
  1. [optional] Create new components as needed.
  2. [optional] Create a new image recipe.
  3. [optional] Create a new infrastructure configuration.
  4. [optional] Create a new distribution configuration.
  5. Create a new pipeline.
Container creation

TODO

Further readings

Sources