Tutorial Dojo - DVA-C02 - Timed Mode Set 4

Test time: Dec 8 2023, 10h00 - 10h50 (00:50:00)

Score: 60/65 (92.3%):

  • CDA – Development with AWS Services 91.67% (33/36)
  • CDA – Security 90.91% (10/11)
  • CDA – Deployment 80% (4/5)
  • CDA – Troubleshooting and Optimization 100% (13/13)

Domain 1: Development with AWS Services

NoQARef
1Automatically watermark images uploaded to S3Use S3 Event Notification s3:ObjectCreate:Put. Send the event to Lambda function (destination)
2Lambda function: access RDS in private subnetConnect to the VPC
3API Gateway + Lambda: Map incoming request to integration request & vice versaAWS custom (Lambda custom)
4Which cache strategy ensured cached data is always up-to-date & stale data are automatically deleted?Write-through + With TTL1.4
5DynamoDD: How to prevent data overwritten?Optimistic locking + Conditional writes (check version number)
6Database credential: How to encrypted & auto-rotate?Use Secrets Manager + Enable Automatic Rotation
7S3: Use SQL to filter & retrieve only a subset data of an object?S3 Select (supports CSV, JSON, Apache Parquet & compressed CSV, JSON)1.7
8DynamoDB - GSI: Consumed throughput; Consistency?GSI has its own consumed throughput. GSI only supports eventually consistent read
9DynamoDB - GSI: How much throughput to provision?GSI WCU >= Base table WCU
10DynamoDB: 2KB items - 10 writes/s - 20 eventually consistent reads/s. How much RCU/WCU?2KB ➡️ 1 strongly consistent read = 1 RCU ➡️ 1 eventually = 0.5 RCU ➡️ 20 eventually = 10 RCU
2KB ➡️ 1 write = 2 WCU ➡️ 10 writes = 20 WCU
111 item = 17KB. 320 strongly consistent read/s. How much RCU?17KB/item ➡️ 5RCU/strongly consistent read ➡️ 320 strongly read = 320 x 5 = 1600 RCU
12HTML, JS, image, video. How to server with lowest latency around the world?S3 + CloudFront
13AI-based app built with Lambda. How to modify the way invocation event are read form Lambda runtime API?Use Lambda custom runtime
14Migrate monolith on-premise app to Lambda. Best practice?1. Take advantage of execution runtime; 2. Use environment variable…1.14 Lambda Best Practice
15ECS: Tasks are scheduled on instances with enough resources. Which task placement strategy?Random
16Online game - Sync app pref + state of player + Allow multiple player to share stateApp Sync (Cognito Sync not support shared data)
17AWS CLI in EC2 instance. How to easily switch role?1. Create a new CLI profile with credential; 2. Run aws CLI with --profile
18DynamoDB: Ensure item is updated only some attribute meets some conditionConditional writes
19DynamoDB: Debug throughput of both base table & GSI when update item?Call UpdateItem with ReturnConsumedCapacity set to None / Total / Indexes1.19 PutItem - ReturnConsumedCapacity
20DynamoDB Streams: How to integrate with Lambda?1. Create an EventSourceMapping to poll the DynamoDB stream, read & process records
2. Give Lambda function enough permission to interact with DynamoDB via ExecutionRole (AWSLambdaDynamoDBExecutionRole managed policy)
21Serverless app: Which service can manage configuration & deploy the whole stack + simple?AWS SAM (Serverless Application Model)
22DynamoDB: Table: ArticleName (PK) - Category (SK). Query ArticleName using another Sort Key + Strongly consistent readCreate a new table with Local Secondary Index (LSI). Migrate the existing data to new table (LSI cannot be created after the DynamoTB is created)
23S3: Ensure all objects are encrypted with AE256Use bucket policy to deny any Create request doesn’t have x-amz-server-side-encryption: AE256 header
24EC2: Shell script to get instance public/private IPUse Instance Metadata Service endpoint 169.254.169.254/latest/meta-data
25Lambda: Account concurrency limit 2000; 10 functions: 1 function 400, 1 function 200. The rest, the third?The rest: 1400; the third: 1300.
26Coordinate multiple services into serverless workflow. Which service?AWS Step Functions
27API Gateway: Enable caching. How to invalidate 1 key?Send the request with Cache-Control: max-age=0
28Lambda: Connection refused. Why?Maybe the invoke URL is wrong (http without s)
29Lambda: Improve performance?Increase memory will increase CPU too.
30Lambda: A function initialize DB connection every time it executes. How to optimize?Move the DB connection to shared execution context (outside handler)
31Lambda: Can the function built with Rust?Yes. Use custom runtime
32Lambda: A function fetch 20MB static data every time it executes. How to optimize?Place the initialize outside Lambda handler; saved external file to /tmp
33Deploy containerized apps? ECS, EKS or Elastic Beanstalk?Under the hood, Elastic Beanstalk uses ECS (& ELB, ASG)
34Online game. How to add feature to cross-sync profile data between device?Use Cognito Sync (or App Sync)
35DynamoDB: Which attribute to use as partition key?The partition should uniquely identify each item
36CodePipeline: Push build details into a DynamoDB?Use EventBridge & Lambda

1.4 Caching Strategies

What is it?ProsCons
Lazy-loadingLoads data into the cache only when necessary (catches cache misses on reads)Only requested data is cachedCache miss read penalty (3 trips)
Stale data
Write-throughLoads data into the cache whenever data is written to the database (populates data on writes)No stale dataMissing data (empty nodes)
No read penaltyCache churn (not re-used cache)
Lazy-loading (with TTL)An expired key is treated as not found
Write-through (with TTL)

Domain 2: Security

NoQARef
11 bucket - many users. How to redact PII & manage access permission?Use S3 Object Lambda (+) Access Point
2SSM Parameter Store: Notify if param haven’t been rotated for 90 days1. Use Advanced tier / Parameter polices / Notification policies
- NoChangeNotification After xxx
- ExpirationNotification Before xxx
- Expiration
2. Use EventBridge rule to filter & send NoChangeNotification event to a SNS topic (target)
3S3: Ensure data is encrypted at rest using the company key1. Client-Side Encryption (CSE) - Encrypt the data before send to S3
2. Server-Side Encryption (SSE) - Send the data with the encryption key
2.1 For CLI, Use the request headers: x-amz-server-side​-encryption-customer + algorithm/key/key-MD5
2.2 Or use the SDK
4KMS: Locally encrypt data (Envelope encryption)1. Use the GenerateDataKey API to get a data key.
2. Use the plaintext data key (GenerateDataKey response Plaintext) to encrypt data locally, then erase the plaintext data key from memory.
3. Store the encrypted data key (GenerateDataKey response CiphertextBlob) alongside the locally encrypted data.
5API Gateway: Custom authorizer using bearer token (same as SAML, OAuth). How?Use API Gateway Lambda authorizer (aka custom authorizer)2.5
- Token-based Lambda authorizer (aka TOKEN authorizer) 👈 THIS ONE
- Request parameter-based Lambda authorizer (aka REQUEST authorizer)
6Database credential + RotateSecrets Manager + Auto Rotation (How? Secrets Manager run a Lambda function )2.6
7CloudFormation: Retrieve license key + cost-effectiveSystems Manager Parameter Store + Secure String
8Migrate on-premise to AWS. Integrate LDAP directory service (not compatible with SAML)Implement a custom identity broker, which use STS to issue short-live AWS credentials2.8
9Cognito: Additional authentication methodIntegrate Multi-Factor Authentication (MFA) to Cognito User Pool
10SQL Server. Migrate to RDS. Encrypt data before write to disk & vice versa.Enable SQL Server Transparent Data Encryption
11S3. Someone use your image without permission.(Block public access) Use pre-signed URL / pre-signed cookies

Domain 3: Deployment

NoQARef
1SAM deploy process (From local machine)1. Build (local); 2. Package (“Publish” to S3); 3. Deploy (Use artifact on S3)
2Elastic Beanstalk: HA, revert quickly?1. Use any EB deployment strategy, e.g. All-at-once (fastest)
2. Blue-Green deployment (EB calls it swap environment URLs)3.2
3CloudFormation: Inline code in templateAWS::Lambda::Function / Code / ZipFile
4Serverless app: Zip code, upload to S3, produce package deployment-ready template & deploysam deploy (which includes sam package)
5CloudFormation: Install packages, start services on EC2 after provisionedcfn-init

3.1 SAM deploy process

npmsam
npm initsam init
npm install
npm buildsam build
npm publishsam package (deprecated, use sam deploy)
npm startsam deploy
- sam list endpoints
- curl http//:localhost:3000- curl https//<>.execute-api.<>.amazonaws.com
nodemon index.jssam sync –watch

sam deploy now implicitly performs the functionality of sam package

Ref:

Domain 4: Troubleshooting and Optimization

NoQARef
1Send traces to X-ray?Use X-Ray daemon (CloudWatch Agent can do this on EC2/On-Premise)
2SQS: Duplicate message. Fix?Use FIFO queue + provide deduplicationID
3X-Ray: Filter trace?1. Use web console; 2. Use GetTraceSummaries (support search)
4X-Ray: Send trace to X-Ray?- 1. Use X-Ray SDK (through X-Ray daemon); 2. Use X-Ray/CLI (directly)
- PutTraceSegments API accepts a single segment document with many segments
5Kinesis: Over-provision. Scale in?Merge cold shards
6API Gateway: Terminated Lambda. Why?- Lambda timeout: max 15min ➡️ terminated
- API Gateway timeout: 30s for an integration ➡️ 504 Timeout
7API Gateway: No metrics for CacheHitCount/CacheMissCountAPI Gateway caching is not enabled
8SQS injections, XSS attack. How to deal?Use Web Application Firewall (WAF). It works with: CloudFront, ALB, API Gateway (REST API)
9Kinesis: 10 shards - 10 EC2 instance. Increase to 20 shards, how many instances?20 instances, the number of instances match the number of shards by 1:1 ratio
10RDS: Monitor memory, CPU usages of processes?Use RDS enhanced monitoring
11CodePipeline: Code review in each stage before move to next stageUse a “manually approval” action, and send the approval request to a SNS topic
12X-Ray: Record call to DB, other services, SQL queries & filterAdd annotations in the subsegment section of the segment document.
13X-Ray: Permission to send trace to X-Ray?AWSXRayDaemonWriteAccess

4.1 How to send traces to X-ray?

  1. X-Ray daemon

    The AWS X-Ray daemon is a software application that listens for traffic on UDP port 2000, gathers raw segment data, and relays it to the AWS X-Ray API.

    The daemon works in conjunction with the AWS X-Ray SDKs and must be running so that data sent by the SDKs can reach the X-Ray service.

  2. AWS Distro for OpenTelemetry (ADOT) Collector

  3. (For EC2, On-premises) CloudWatch Agent can do this from 1.300025.0

4.1 Setup X-Ray daemon

  • Lambda: the deamon run automatically
  • EC2:
    • Elastic Beanstalk: Enable with XRayEnabled configuration option
    • EC2: Install/Run manually (May use user data)
    • ECS:
      • Use official Docker image
      • Build custom Docker image
  • On-premise: Install/Run manually

4.2 SQS: Duplicate messages

https://aws.amazon.com/about-aws/whats-new/2016/11/amazon-sqs-introduces-fifo-queues-with-exactly-once-processing-and-lower-prices-for-standard-queues/ https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html#SQS-SendMessage-request-MessageDeduplicationId

4.8 AWS WAF, AWS Firewall Manager, and AWS Shield Advanced

https://docs.aws.amazon.com/waf/latest/developerguide/what-is-aws-waf.html

4.9 Scale Kinesis Data Streams

To scale up processing in your application, you should test a combination of these approaches:

  • Increasing the instance size (because all record processors run in parallel within a process)
  • Increasing the number of instances up to the maximum number of open shards (because shards can be processed independently)
  • Increasing the number of shards (which increases the level of parallelism)

https://docs.aws.amazon.com/streams/latest/dev/kinesis-record-processor-scaling.html

4.10 Monitor RDS

  • CloudWatch (default): RDS automatically sends metrics to CloudWatch every minute (from hypervisor for the DB instance - “outside”)
  • RDS Enhanced Monitoring:
    • Gather metrics about the OS: processes, memory… (from an agent on the DB instance - “inside”)
    • Logs are sent to CloudWatch Logs

Differences between CloudWatch and Enhanced Monitoring metrics

4.11 Manage approval actions in CodePipeline

https://docs.aws.amazon.com/codepipeline/latest/userguide/approvals.html