[[TOC]]
Architecture Deep Dive
Event Driven Architecture is an upgrade to a monolithic architecture.
- Monoliths fail together scale together, and bill together.
Tiered architecture splits up the monolith into different platforms for each of the stages of the application. These are still coupled together, but could be a bit more elastic. Each of these tiers can then be scaled independently. If we place load balancers between these tiers, we can create more elasticity. Tiered architectures create a dependence on the other tiers.
Queues
Queues decouple architecture.
- When the upload is complete, it stores the video in S3 and adds a message to the queue. It doesn't pass anything to the processing tier, nor does it even know it's running. It simply adds a message that says "there's something in S3 that needs processing."
- Other people may be uploading videos as well and those all do the same thing, go into S3 and a message gets added to the queue
- An autoscaling group sits alone and is triggered by the queue length. If there are two videos in the queue, it spools up 2 EC2 instances and then processes the message from the queue which tells the instance where the video is, and what processing needs done to it.
- The instance then processes this video. Once the videos are completed, the message is deleted from that queue and then the autoscaling group scales ini accordingly to how many messages are in the queue, which is zero at this point.
Microservices
These further decouple architectures by having services that fall under 3 categories
- Producers
- Consumers
- or Both
Event Driven Architecture
Event Producers - created when something happens. When something is uploaded or submitted. Event Consumers - these are created when there is something to consume or process Event Routers - these move things from producers to consumers Event Bus - a constant stream of data.
Nothing is sitting there running and waiting Once the actions are taken, it goes back into waiting state. These only consume resources while handing events - serverless :)
AWS Lambda in Depth
Lambda is a FaaS product - Function as a Service - short running and focused. Lambda function is a piece of code that lambda runs These functions use a runtime (Python 3.8) Lambda loads functions and runs them in that runtime environment. You are only billed for the duration that a function runs. Lambda is a key part of serverless architecture.
Deployment package is downloaded and executed in the runtime environment Different languages (Python, Ruby, Go, Java) NOT DOCKER. That is Container computing.
Assume that every time a Lambda function is invoked, that it is running in a new runtime environment. Tips:
- Max time: Lambda has a 15minute timeout.
- Lambda is given public networking by default so that it can access public AWS services and the public Internet.
- No customer specific VPC networking is required
- Lambda functions have no access to VPC based services unless Public IPs are provided and security controls allow external access.
- Lambda functions that run in a VPC obey all the VPC networking rules.
- Cannot access outside the VPC without networking or VPC endpoints to the public services.
- NATGW and IGW are required for VPC Lambdas to access Internet resources.
- Treat lambda functions running inside VPCs as anything else that runs inside the VPC.
Security
Lambda has resource policies that control what services and accounts can invoke the lambda functions. Lambda execution roles are IAM roles attached to lambda functions which control the permissions that the lambda function receives.
Logging
CloudWatch, Cloudwatch logs, and X-Ray Logs from execution - Cloudwatch logs. Metrics - stored in Cloudwatch Lambda can be integrated with X-Ray for distributed tracing. Cloudwatch logs requires permissions via the execution role.
Invocation
- Synchronous - CLI or API invoke lambda function and waits for a response. Response responds or fails. Response fails during that request.
- Asynchronous - typically used when AWS services invoke lambda functions. Function code needs to be idempotent.
- Event source mapping - Typically used on streams or queues which don't support event generation to invoke Lambda (Kinesis, DynamoDB streams, SQS) Event source mapping pulls a source branch and then sends that to Lambda as an event batch. All must succeed or the entire event batch fails. Permissions from the lambda execution role are used by the event source mapping to interact with the event source.
Versions
Immutable $latest - latest version of the function Aliases.
Lambda start up times
An execution context is the environment that a Lambda function runs in.
- A cold start is the full creation and configuration of a lambda function, including the code download.
- a warm start is the reuse of an execution context. If too much time passes, the execution context is reset. A Lambda function can reuse an execution context but has to assume that it can't. You can't write in something that expects code to already exist in a context.
- You can pre-provision these execution contexts to save time as well.
CloudWatch Events and EventBridge
If something happens or we want something done at a specific time, do something.
- Use EventsBridge over CloudWatch Events. It's basically V2 of CloudWatch Events.
- there is a default event bus for the account
- Cloudwatch has one bus (implicit)
- EventBridge can have multiple event busses.
- Rules match incoming events or match the schedule (CRON jobs)
- routes 1 or more targets such as Lambda.
Default Event Bus
- EC2 state stops, then an event is generated and placed into the Event Bus.
- If there is a rule that matches EC2 state stoppage, it will pull the JSON payload from the Event Bus and pass it on to the target - Lambda to do something as a response to that event.
Demo - Automated EC2 Start/Stop and protect using Lambda
Demo
Serverless Architecture
Serverless is not just one single thing.
- Applications are a collection of very small and specialized functions
- These run in stateless and ephemeral environments
- Everything is Event Driven - runs only when something is being used
- FaaS is used wherever possible for compute functionality.
- Managed services are used wherever possible.
Example
SNS - Simple Notification Service
Public AWS Service - network connected with Public Endpoint Coordinates the sending and delivery of messages Messages are greater than or equal to 256kb payloads.
- Publisher sends messages to a TOPIC
- Subscribers receive messages from a TOPIC.
- These can be HTTP(s) Email, SQS, Mobile Push, SMS message, or Lambda.
- You can also publish to a subscribing API that also publishes to a TOPIC
- FANOUT is when you publish a single message to many SQS queues
Delivery status tracks the success of the delivery Delivery retries ensure reliable delivery HA and Scalable Capable of SSE Cross account via TOPIC Policy
Step functions
15 minute max execution time
- can chain lambda functions together