Lesson 2 EC2 and IAM
Lesson 2 EC2 and IAM
2
Main points
• The big picture – High level architecture of the AWS Cloud. Concepts
include
• Physical – AZs, edge servers
• Logical – regions, VPC, subnets, and IP addresses.
• Virtual machine (EC2) – Its components such as AMI, EBS,
Snapshots, SG.
• Identity access management (IAM) – users, roles, permissions, and
STS assume role.
AWS Global Infrastructure Map
Read more about: AWS Global Infrastructure and AWS Global Accelerator features
AWS Global Infrastructure
Regions (Logical) - A physical location
around the world where AWS clusters
data centers. One region has multiple
data centers.
Availability Zones (Physical) -
Geographical isolated data centers
within a region.
Regions
• Followings vary depending on the region.
• Service and service feature availability – Check out AWS announcement page to
see what services and features are newly added in that region.
• Limit – See next slide
• Pricing – The same servers in the US region tent to cost less than the ones in Asia.
• Most services are regional. To build and deploy a global app, you have to
deploy the same stack to every region. Most companies target 3 regions
in 3 different continents.
• If the region goes down, all apps in that region are down as well. So,
architects consider a multi-regional deployment strategy.
Availability Zones provide redundancy for AWS resources in that region, highly available, fault tolerant, and more
scalability. AZs have low latency, high-bandwidth network connection, and support near real-time replication
between AZs. All traffic is encrypted.
High Availability is creating an architecture in such a way that the system is always available or has the least
amount of downtime as possible. If the app is running on 2 or more AZs, it means the app is highly available.
Availability is normally expressed in 9’s.
• 5 nines uptime means only 5 min downtown is allowed a year
• 4 nines uptime means less than an hour downtown is allowed a year Region
Fault Tolerant is the ability of your system to withstand failures in one or more of AZ #1
its components and still remain available. Asynchrony and decoupling using SQS
increase fault tolerance.
AZ #2 AZ #3
Edge servers
AWS has servers in 247+ Countries and Territories. Those servers are
not many in that area to form an AZ or a region. Those servers are
called edge servers. Edge servers play the following roles:
• AWS Global Accelerator takes advantage of edge servers and routes
requests efficiently in the AWS network without bouncing on the
internet.
• CloudFront caches your static contents all over the world on edge
servers.
• You can run small code on Lambda@Edge which is a feature of
CloudFront.
AWS Accelerator is taking advantage of edge servers to improve performance.
Building a global app with AWS Global Accelerator
VPC
Virtual Private Cloud (VPC)
VPC is an isolated virtual network where non-public AWS resources
run. You have complete control over your networking such as the
selection of your own local IP address range, the creation of subnets,
and other networking components.
In other words, VPC is simply how you manage networking in the cloud.
VPC is similar to traditional networking. VPC is mostly managed by a
networking guy or the DevOps team. You just need to know its high
level, big picture concepts.
2 types of resources
1. Public – It is just like a third-party API that you can call directly such as S3,
DynamoDB, SNS, SQS, and so on (FaaS services). If valid authorization
tokens are present, the API call is a success.
2. Non-public – You must launch those resources in VPC so that it gets an IP
address so others can connect to it. It includes EC2, RDS, Load Balancers,
and so on (IaaS and PaaS services).
Subnets
A subnet is a sub-section of a network. Generally, it includes all the
computers in a specific location like zip code for addressing houses.
A VPC includes many subnets. A subnet is associated with an AZ.
There are 2 types of subnets:
• Anyone can access to resources in public subnet directly from the internet.
• A private subnet is a safe environment where you can run back-end servers
and databases securely. The internet (outsiders) cannot directly access
resources in private subnet. The only way to access private resources is
through the other resource in the public subnet. All resources in VPC talk to
one another using private IPs even if they all have public IPs.
Subnets are written in CIDR format.
Read more about Subnets
CIDR ranges for the private network
Most popular private IPs
CIDR Starting and ending IPs Total number of hosts
To access your resource in the VPC, you must allow access on both layers.
You don’t normally touch the NACL whereas the SG is the most important
concept like IAM that developers work daily basis. If the resource is not
responding, most likely, it is an issue with your SG that doesn’t allow
incoming access.
Network Access Control Lists - NACL
NACL acts as a firewall for controlling traffic in and out of one or more
subnets. Your default VPC already has an NACL in place and is
associated with all default subnets.
To access an EC2 instance from the internet, the request must pass the
NACL security layer. If there is a rule that denies the request on the
way, you cannot access the instance. Once it passed the NACL layer,
then the request also has to pass Security Groups (SG) layer associated
with the instance.
Read more about Network ACLs
The default NACL allows
all traffic, both inbound Network Access Control List - NACL
and outbound
EC2 EC2
Subnet 1 (public) Subnet 2 (public)
NACL Rules
• The default NACL allows all traffic to the default subnets.
• Rules are evaluated from lowest to highest based on rule #. The first rule
found that applies to the traffic type is immediately applied, regardless of
any rules that come after it.
• The NACL allows or denies traffic from entering a subnet. Once inside the
subnet, other AWS resources may have additional security layers (security
groups).
NACL
Inbound Outbound
Security Group
EC2 Instance
NACL Security Group
NACL can be understood as the firewall or Security group can be understood as a firewall to
protection for the subnet. protect EC2 instances.
These are stateless, meaning any change applied to These are stateful, which means any changes which
an incoming rule isn’t automatically applied to an are applied to an incoming rule is automatically
outgoing rule. Example: If a request comes through applied to a rule which is outgoing. Example: If the
port 80, it should be explicitly indicated that its incoming port of a request is 80, the outgoing
outgoing response would be the same port 80. response of that request is also 80 (it is opened
automatically) by default.
NACL supports allow and deny rules. Denial of rules SG supports only allow rules, and the default
can be explicitly mentioned, so that when the layer behavior is denial of all.
sees a specific IP address, it blocks connecting to it.
In case of NACL, the rules are applied in the order of In case of a security group, all the rules are applied
their priority, wherein priority is indicated by the to an instance.
number the rule is assigned. This means every rule
is evaluated based on the priority it has.
EC2
AWS Elastic Cloud Compute (EC2)
EC2 is a web service that provides resizable compute capacity in the
cloud. It is designed to make web-scale cloud computing easier for
developers.
Basically, a virtual computer, very similar to the desktop or laptop you
use at home, and commonly referred to as an instance.
You can use Amazon EC2 to launch as many or as few virtual servers as
you need, configure security and networking, and manage storage.
Amazon EC2 autoscaling enables you to scale in or out to handle
changes in requirements or spikes in popularity, reducing your need to
forecast traffic.
Read more: Amazon EC2
Computer and EC2 Instance
Firewall
Operating System CPU & RAM Hard Drive Network Adapter Security Groups
AMIs Instance Type EBS ENI
(Linux or Windows)
Amazon Machine Image - AMI
Preconfigured and required to launch an EC2 instance that
includes an operating system, software packages, and other
required settings.
Amazon Machine Image (AMI) provides the information required to launch
an instance. You specify an AMI when you launch an instance.
In class, I have limited time to demonstrate all components of EC2. You can
practice all at home if you are dedicated. Always highly encourage you to
play with the AWS console, read descriptions and experiment with all the
things you see on the AWS EC2 console. Similarly, be bold and play with all
services. You have a free account.
Read more: AMI
Custom AMI
Assume that you need to run your app in hundreds of servers. It is not
possible to configure every single server one by one. Instead, you can
create a custom AMI and use it on as many servers as you want.
Custom AMIs contain all configurations, dependencies, and
environments required to run your app.
Understanding AMI
My EC2 Instance #1
Quickly create
additional Instances
My EC2 Instance Create AMI using the AMI
Custom Installation My EC2 Instance #2
My EC2 Instance #3
Instance Types
The Instance Type determines the underlying hardware such as CPU, GPU,
RAM, network and disk read/write capacity.
Each instance type offers different compute, memory, and storage, graphic
capabilities and is grouped into instance families based on these capabilities.
NVIDIA instances have a GPU (accelerated computing) that works best for ML
since a GPU provides high parallel and simultaneous computing.
The same service price varies depending on the region. For example, the
same instance in the USA costs higher than the one in Japan.
With EC2 (IaaS), you have more control such as dictating the underlying
hardware. Whereas in Lambda (FaaS) and ECS (Container as a Service), you
can not control it.
Read more: Instance Types
General-purpose and memory-optimized
instance type
General-purpose instances provide a balance of compute, memory,
and networking resources. It can be used for web servers and code
repositories. General-purpose instance types start with T and M.
EC2 Instance
EBS Additional Volume
Snapshots
A snapshot is an image of an EBS volume that can be stored
as an incremental backup of the volume or used to create a
duplicate.
EBS
A snapshot is not an active EBS volume. You cannot attach or
detach a snapshot to an EC2 instance.
To restore a snapshot, you need to create a new EBS volume
using the snapshot as its template.
Snapshot
AMI vs Snapshots
Group
IAM User User
developers
George Mike
George
Mike
S3 Full Access
S3
Policy
AWS IAM Role
IAM Role
S3 Full Access
Policy
S3
EC2
IAM Identity Center
AWS started recommending to use the IAM Identity Center for giving
AWS access to developers. IAM Identity Center benefits:
• Multi-account management
- Better cost management
- Better access management (read-only to production)
- Remediate service quote per account
• Single Sign On (SSO) – Not only AWS, you can also access popular
apps such as Slack, Asana, GitHub, so on
Two ways to allow/deny access in IAM
1. Identity-based policies – Attach policies to IAM identities (users, groups,
or roles) as we talked about in the previous 3 slides.
2. Resource-based policies – Attach inline policies to resources (such as S3,
SQS, SNS) directly. Resource-based policies have a principal tag that
defines who can the actions defined.
If you want to connect EC2 with S3, you can do it in 2 ways. Add a role to
EC2, that is what identity-based authorization is. Alternatively, you can write
a resource-based policy on the S3, that is what resource-based authorization
is.
It is confusing in the beginning if you use identity or resource-based
authorization. Confusion is a great ingredient of growth. It comes naturally
with an experience. After a year, it will become easy.
IAM Policy structure
IAM JSON policy elements: Condition
The Condition element (or Condition block) lets you specify conditions for when
a policy is in effect.
In the Condition element, you build expressions in which you use condition
operators (equal, less than, etc.) to match the condition keys and values in the
policy against keys and values in the request context.
Learn more: IAM JSON policy elements: Condition
For instance, when fetching images from S3 in EC2, EC2 makes the AssumeRole
call to STS, then receives the token and provides the token to S3. You don’t have
to manually rotate it. The rotation is done by AWS.
IAM User vs Role
IAM User
IAM Role
IAM entity assigned to a person. IAM entity assigned to a service. It has a trust policy
that specifies what services can use the role.
Tokens are permanent. Not recommended. Tokens are temporary. Tokens are generated by
AWS STS. It has an extra token “aws_session_token”.
Identity federation
Identity federation grants external identities secure access to resources in your
AWS account. These external identities can come from your corporate identity
provider such as Microsoft Active Directory and Instructure (AWS Academy).
Federated users (external identities) are users you manage outside of AWS in
your corporate directory, but to whom you grant access to your AWS account
using temporary security credentials (Role and STS). AWS Academy is an
example.
Federated users and temporary security credentials STS
IAM Summary
IAM is about:
1. allow/deny
2. what actions?
3. on which resources?
4. who?
a. principle propriety (resource-based)
b. IAM user or role (identity-based)
5. condition (optional)