AWS: CDA Flashcards
What is Elastic Beanstalk?
- Developer-centric view of deploying an app on AWS
- Fully managed service
- Handles capacity provisioning, load balancing, scaling, monitoring
- Free service but underlying AWS resources will have costs involved
What are the components involved in Elastic Beanstalk?
- Application
- Collection of components
- Application version
- Iteration of app code
- Environment
- Collection of AWS resources running an app version
- Env tiers to support diff types of apps
- Multiple env can be created ie. dev/prod
What are the use cases for the different environment tiers?
- Web server environment tier
- Website
- Web app
- Web app serving HTTP requests
- Worker environment tier
- Processing long-running workloads on demand
- Perform tasks on a schedule
List the different deployment options available for Elastic Beanstalk
- All-at-once deployment
- Fastest
- Instances are down during update
- No additional cost
- Rolling deployment
- Zero downtime
- Deployment time depends on number of instances
- No additional cost
- Rolling deployment with additional batches
- Zero downtime
- Deployment time depends on number of instances
- Small additional cost
- Immutable deployment
- Zero downtime
- Longest deployment
- High cost, double capacity
- Quick rollback in case of failure
- Blue/green deployment
- Zero downtime
- Traffic-splitting deployment
- Zero downtime
- Quick rollback in case of failure
How do rolling deployments work?
- Apps running below capacity with set bucket size
- Instances in the bucket will be down during update
- Once instances in the bucket are updated, process repeats for next bucket (batch of instances)
How does rolling deployments with additional batches work?
Deployments
- App running at capacity with set bucket size
- New instances created with upgraded version
- Existing instances go through rolling deployments
- After rolling deployment is complete, the newly-created instances will be terminated
How does blue/green deployments work?
- Create a new environment (green) where new version is deployed
- Easy rollback to old environment (blue) if issues arise
How do traffic-splitting deployments work?
Deployments
- Used for canary testing
- New app version deployed to temp ASG with same capacity
- Small % of traffic sent to temp ASG for a configurable amount of time
- New instances migrated from temp to original ASG and then old version is terminated
- Automated rollback if issues arise
What is the lifecycle policy for Elastic Beanstalk?
- A configurable policy to limit no. of app versions to retain for future deployments
- Limit by count
- Limit by age
- Must be enabled first to configure policy
What happens under the hood for Elastic Beanstalk?
- Relies on CloudFormation
- CloudFormation is infra as code
- Used to provision other AWS services
What is Elastic Beanstalk cloning?
- Clone an environment with exact same configuration
- All resources and config are preserved
- After cloning an environment, you can modify settings
- Useful for deploying a “test” version of your app
What is API Gateway?
- Serverless service to manage and secure APIs
- A single interface for all microservices
- Use API endpoints with various resources
- Apply forwarding and transformation rules at API Gateway level
What are some features of API Gateway?
Development
- Support websocket protocols
- Transform and validate requests/responses
- Handle request throttling
- Cache API responses
- Handle API versioning
- Handle different environments
- Handle security
What are the different endpoint types for API Gateway?
- Edge-optimised (default)
- Requests routed through CloudFront Edge locations to improve latency
- API Gateway still only lives in one region
- Regional
- For clients within same region
- Could be manually combined with CloudFront
- Private
- Only accessed from your VPC using interface VPC endpoint
- Use resource policy to define access
What are the user authentication strategies available for API Gateway?
- IAM roles
- Useful for internal applications
- AWS Cognito
- Useful for external users
- Custom authoriser (your own logic via Lambda function)
How can you have security with your own custom domain name by integrating API Gateway with ACM?
- If using edge-optimised endpoint, certificate must be in us-east-1
- If using regional endpoint, certificate must be in API Gateway region
- Must setup CNAME or A-alias record in Route 53
What are stage variables in API Gateway?
Development
- Similar to environment variables
- Used to update frequently changing config values
- If used in Lambda functions, they are passed to the “context” object
What are the use cases for stage variables in API Gateway?
- They can be used in:
- Lambda function ARN
- HTTP endpoint
- Parameter mapping templates
- Use cases:
- Configure HTTP endpoints that the stages talk to (dev, test, prod)
- Pass config parameters to Lambda functions through mapping templates
How to perform canary deployments in API Gateway?
- Usually done with prod
- Choose % of traffic the canary channel receives
- Metrics/logs are separate for better monitoring
- Stage variables can be overridden for canary deployments
- Once canary deployments have been tested and if all good, they can be promoted to entire stage
What are the different integration types for API Gateway?
- Mock integration type
- API Gateway returns a response without sending a request to backend
- HTTP/AWS services
- Both integration req and res must be configured
- Setup data mapping using mapping templates for req and res
- AWS proxy integration type
- Incoming req from client is the input to Lambda
- Lambda function is responsible for logic of req/res
- No mapping template/headers/query params are passed as arguments
- HTTP proxy integration type
- HTTP req is passed to backend
- HTTP res from backend is forwarded by API Gateway
- No mapping templates
- Optionally add HTTP headers if needed eg. API key
What are mapping templates in API Gateway?
- Templates used to modify req/res
- Rename/modify query string params
- Modify body content
- Add headers
- Filter result output
- To set the template, the content-type must be set to either application/json or application/xml
- Not used for proxy integration types
How can API Gateway utilise request validation?
- Importing Open API definitions
- The spec is used to verify if req corresponds to proper schema before proceeding with req
- If validation fails, API Gateway immediately fails req
- Reduces unnecessary calls to backend
How does caching work in API Gateway?
- Caching reduces number of calls to backend
- API Gateway will first check cache
- If cache miss, call backend
- Default TTL is 300s
- Cache is expensive - makes sense for prod but may not make sense for dev/test
What is the difference between latency vs integration latency in API Gateway?
- Integration latency
- Time between when API Gateway relays req to backend and receives a response from backend
- Latency
- Time between when API Gateway receives req from client and when it returns response to client
- Includes integration latency and other API Gateway overhead
What are WebSockets?
- Two-way interactive communication between user’s browser and a server
- Server can push information to client
- Enables stateful application use cases
- Often used for real-time apps
How does API Gateway handle WebSocket routing?
- API Gateway uses a route key table that incoming JSON messages are evaluated against
- If no routes, sent to $default
- Route is then connected to the backend setup through API Gateway
What are the different security strategies for API Gateway?
- IAM
- Great for existing users/roles in AWS
- Authentication via IAM
- Authorisation via IAM policies
- Can be combined with resource policies for cross-accounts
- Leverages sig 4 where IAM creds are in headers
- Custom authoriser
- Great for 3rd party tokens
- Authentication via 3rd party system but verified in Lambda
- Authorisation via Lambda fn
- Lambda must return IAM policy for user - result is cached
- Cognito User Pool
- Great for fully managed service
- Tokens expire automatically
- Authentication via Cognito User Pools
- Authorisation via API Gateway methods
What is DynamoDB?
- Managed NoSQL database
- Highly available with replication across multi AZ
- Scales to massive workloads
- Fast and consistent performance
- Low cost and auto-scaling capabilities
What are features of NoSQL databases?
- Non-relational databases
- eg. MongoDB, DynamoDB
- Distributed
- Scale horizontally
- Do not support query joins/aggregation computations
- All data needed is presented in one row
Describe DynamoDB tables
- Each table has a primary key
- Must be decided at creation time
- Non-null
- Each item has attributes
- Similar to columns but more powerful
- Can be added over time - can be null at creation time
What are the different strategies of choosing a primary key for DynamoDB tables?
Development
- Partition key (hash)
- Unique for each key
- Diverse so data is distributed
- Example: “user_id” for “users” table
- Partition key + sort key
- Combination must be unique for each item
- Data grouped by partition key
- Example: “user_id” for partition key and “game_id” for “users_games” table
What happens when read and write throughout is exceeded for DynamoDB?
- Table must have provisioned read/write capacity units
- Can setup auto-scaling
- Throughput can be exceeded temporarily using burst capacity
- If burst capacity has been consumed, there will be a ProvisionThroughputExceededException
- It’s then advised to do an exponential backoff retry or distribute partition keys
What is the difference between eventually consistent read vs strongly consistent read?
- Eventually consistent read
- Possibility of a lag where data has not been replicated but a read has been made
- Strongly consistent read
- Ensures no data staleness
- Consumes twice the RCUs
What are operations for writing data to DynamoDB?
- PutItem
- Creates new item or replaces old item (same primary key)
- Consumes WCUs
- UpdateItem
- Edit an existing item’s attributes or adds a new item if it doesn’t exist
- Can be used to implement atomic counters
- Conditional writes
- Accepts a write/update/delete only if conditions are met
- Helps with concurrent access to items
What are the operations for reading data from DynamoDB?
Development
- GetItem
- Read based on primary key
- Eventually consistent read (default) and have option to use strongly consistent reads (more RCUs)
- Query
- Returns items based on KeyConditionExpression and FilterExpression
- Ability to paginate results
- Scan
- Scans entire table and then filter data (inefficient)
- Consumes a lot of RCUs
- Use parallel scans for faster performance - consumes significantly higher RCUs
What are the operations for deleting items from DynamoDB?
- DeleteItem
- Deletes individual item
- Ability to perform conditional delete
- DeleteTable
- Delete entire table and its contents
What are the benefits of batching operations in DynamoDB?
- Reduce latency by reducing number of API calls
- Operations are done in parallel for better performance
What are the operations for batching in DynamoDB?
- BatchGetItem
- Returns items from one or more tables
- Items retrieved in parallel to reduce latency
- BatchWriteItem
- Can’t update items
- UnprocessedItems
- Failed write operations
What is PartiQL?
- SQL-like syntax to manipulate DynamoDB tables
- Run queries across multiple DynamoDB tables
- Supports some (not all) SQL statements
- INSERT
- UPDATE
- SELECT
- DELETE
What is the difference between GSI vs LSI?
- Global Secondary Index (GSI)
- Query a specific index that spans all data in base table, across all partitions
- Support eventual consistency only
- Can be added/updated after table creation
- Must provision RCUs and WCUs for index
- Queries or scans on this index consume capacity units from the index, not from the base table
- If writes are throttled, main table will also be throttled
- Local Secondary Index (LSI)
- Only added at table creation
- Uses RCUs and WCUs of main table
- No special throttling consideration
What is optimistic locking in DynamoDB?
- Conditional writes
- A strategy to ensure an item hasn’t changed before it is updated/deleted
What is DynamoDB Accelerator (DAX)?
Fully managed, highly available, in-memory cache for DynamoDB
What are the key features of DAX?
- Microseconds latency for cached reads
- Compatible with existing DynamoDB APIs
- Doesn’t require any modifications to application logic
- Solves the “hot key” problem (too many reads)
- Secure
- Multi AZ
- Min 3 nodes recommended for prod
- 5 mins TTL
- Up to 10 nodes per cluster
What are DynamoDB streams?
- Ordered stream of item-level modifications (create/update/delete) in a table
- Streamed records can be:
- Sent to Kinesis
- Read by Lambda
- Data retention up to 24 hours
- Records are not retroactively populated after it is enabled
What are some use cases for DynamoDB streams?
- React to changes in real-time
- Analytics
- Implement cross-region replication
How do DynamoDB streams work with Lambda?
- Define Event Source Mapping to poll from DynamoDB streams and receive records in batches
- Ensure Lambda function has appropriate permissions to read from stream
- Lambda function is invoked synchronously with batch of records
How does the TTL feature work in DynamoDB?
- Automatically delete items after an expiry timestamp
- Expired items are deleted within 48 hrs
- Deleted from both GSI and LSI
- TTL attribute must be a “number” data type with Unix epoch timestamp value
- Doesn’t consume any WCUs
What are the use cases for enabling a TTL in DynamoDB?
- Reduce stored data by keeping only current items
- Adhere to regulatory obligations
In DynamoDB CLI, what does —projection-expression flag do?
One or more attributes to retrieve
In DynamoDB CLI, what does —filter-expression flag do?
Filter items before being returned
How does the transactions feature work in DynamoDB?
- Co-ordinated all-or-nothing operations
- Provides ACID (atomicity, consistency, isolation, durability)
- Read modes:
- Eventual consistency
- Strong consistency
- Transactional consistency
- Write modes:
- Standard consistency
- Transactional consistency
- Consumes 2x WCUs and RCUs
- Performs 2 operations for every item (prepare and commit)
What are the use cases for transactions?
- Financial transactions
- Managing orders
- Multi-player games
What is the difference between using DynamoDB as session state cache vs ElastiCache or EFS?
- ElastiCache
- ElastiCache is in-memory but DynamoDB is serverless
- Both are key/value stores
- DynamoDB has auto-scaling
- EFS
- Must be attached to EC2 instances as network drives
What are the different write types of DynamoDB?
- Concurrent writes
- Conditional writes
- Atomic writes
- Batch writes
What are some AWS services that can be used to decouple applications?
- SQS
- SNS
- Kinesis
What is SQS?
- Fully managed service that queues messages
- Consist of:
- Producer(s) - sends messages to the queue
- Consumer(s) - polls and processes messages from the queue
What are the different types of SQS queues?
- Standard
- FIFO
What are key features of SQS?
- Unlimited throughput
- Unlimited no. of messages in the queue
- Retention of messages
- Default: 4 days
- Max: 14 days
- Low latency (<10 ms)
- Message size limit of 256 KB/message
- Can have duplicate messages - at least once delivery
- Can have out of order messages - best effort ordering
How does SQS produce messages?
- Send messages using SDK
- SendMessage API
- Message persisted in SQS until a consumer deletes it which signifies that it has been processed
How does SQS consume messages?
Development
- Poll SQS for messages
- Receive up to 10 messages at a time
- Process the messages
- Delete the message using SDK
- DeleteMessage API
- Scale consumers horizontally to improve throughout
- Can have multiple consumers process messages in parallel
How can SQS be used with ASG to increase throughput?
- Have multiple EC2 instances in an ASG consuming SQS messages
- SQS has built-in CloudWatch metrics that can trigger an alarm if messages go over a certain number
- ApproximateNumberOfMessages
- Alarm can trigger ASG to scale
What are the security strategies for SQS?
- Encryption
- In-flight encryption using HTTPS API
- At-rest encryption using KMS keys
- Client-side encryption (client will need to handle encryption/decryption itself)
- Access controls
- IAM policies to regulate access to SQS API
- SQS queue access policies
- Resource policy (similar to S3 bucket policies)
- Useful for cross-account access
- Useful for other services to write to SQS
What does message visibility timeout mean in SQS?
- After message is polled by consumer, it becomes invisible to other consumers
- Default 30s for messages to be processed
- After message visibility timeout lapses, message is then visible again in SQS
What are dead letter queues in SQS?
- If consumer fails to process a message within visibility timeout, then message goes back into the queue
- Threshold can be set to limit how many times a message can go back into the queue
- After threshold (MaximumReceives) is exceeded, message sent to DLQ
- DLQ must inherit its queue type
- DLQ of FIFO queue must also be FIFO queue
- DLQ of standard queue must also be standard queue
- Useful for debugging
- Set expiry time (14 days retention) to process messages before expiry
What is the “re-drive to source” feature of DLQ?
- Help consume messages in DLQ to understand what is wrong
- Allow manual inspection and debugging
- When code is fixed, we can re-send message back into source queue in batches to be reprocessed
- No custom code needed
What are delay queues in SQS?
- Delays a message so consumers can’t receive it immediately
- Default is 0s - message available immediately
- Can be delayed up to 15 mins
- Default can be overridden on send using DelaySeconds parameter
What is long polling in SQS?
- When a consumer requests messages from the queue, it can optionally wait for messages to arrive if there are none in the queue
- Wait time can be 1-20s
- Long polling decreases no. of API calls while increasing latency
- Can be enabled at:
- Queue level
- API level using ReceiveMessageWaitTimeSeconds
What is SQS Extended Client?
- Java library
- Used to send large messages (ie. 1GB) due to standard size is 256 KB