1 of 74

Module 10: Developing with Messaging Services

AWS Academy Cloud Developing

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

2 of 74

Section 1: Introduction

Module 10: Developing with Messaging Services

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

3 of 74

Module objectives

At the end of this module, you should be able to do the following:

  • Illustrate how messaging services, including queues, pub/sub messaging, and streams, support asynchronous processing
  • Describe Amazon Simple Queue Service (Amazon SQS)
  • Send messages to an SQS queue
  • Describe Amazon Simple Notification Service (Amazon SNS)
  • Subscribe an SQS queue to an SNS topic
  • Describe how Amazon Kinesis can be used for real-time analytics

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

3

4 of 74

Module overview

Sections

  1. Introduction
  2. Processing requests asynchronously
  3. Introducing Amazon SQS
  4. Working with Amazon SQS messages
  5. Configuring Amazon SQS queues
  6. Introducing Amazon SNS
  7. Developing with Amazon SNS
  8. Introducing Kinesis Data Streams

Demonstration

  • Working with Amazon Messaging Services

Lab

  • Implementing a Messaging System Using Amazon SNS and Amazon SQS

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

4

Knowledge check

5 of 74

Café business requirement

Currently, updating the coffee inventory on the website is a manual process. Sofía would like to find an easier way to keep the inventory updated. She considers setting up a messaging system to automatically receive and process inventory updates.

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

5

6 of 74

Messaging services as part of developing a cloud application

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

6

7 of 74

Section 2: Processing requests asynchronously

Module 10: Developing with Messaging Services

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

8 of 74

Synchronous processing

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

8

Tightly coupled

Client

Service A

Service B

Response/result

Request

9 of 74

Asynchronous processing

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

9

Client

Service A

Service B

Acknowledgement

Request

The result might be returned to the client through another process

10 of 74

Asynchronous processing�with messaging services

Message queues

Pub/sub messaging

Data streams

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

10

Amazon SQS

Amazon SNS

Amazon Kinesis�Data Streams

11 of 74

Message queues overview

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

11

Queue

Producers

Consumer

Poll for messages

Add messages to the queue

Process each retrieved message

Delete the processed message from the queue

1

2

3

4

12 of 74

Pub/sub messaging overview

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

12

Topic

Producers

Subscriber A

Push messages to subscribers

Subscriber B

Subscriber C

Add messages to a topic

Process messages of interest

1

2

3

13 of 74

Data streams overview

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

13

Stream

Producers

Consumer

Poll for messages

Add messages to the stream

Aggregate and analyze large numbers of messages

1

2

3

14 of 74

Section 2 key takeaways

  • An asynchronous design reduces interdependencies and can improve responsiveness to the client.
  • Messaging services are good for creating asynchronous designs and include queues, pub/sub messaging, and streams.
  • AWS provides fully managed messaging services including Amazon SQS, Amazon SNS, and Kinesis Data Streams.

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

14

15 of 74

Section 3: Introducing Amazon SQS

Module 10: Developing with Messaging Services

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

16 of 74

Amazon SQS

Fully managed message queuing service that eliminates the complexity and overhead associated with managing and operating message queues

  • Limits administrative overhead
  • Reliably delivers messages at very high volume and throughput
  • Scales dynamically
  • Can keep data secure
  • Offers standard and First-In-First-Out (FIFO) options

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

16

Amazon Simple Queue Service (Amazon SQS)

17 of 74

Standard and FIFO queue types

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

17

Standard (default)

FIFO

Best effort ordering, at-least-once delivery. Nearly unlimited throughput.

Message order is preserved, exactly-once delivery. More limited throughput.

Use cases

Use cases

  • Let users upload media while resizing or encoding it
  • Ensure that a deposit is recorded before a bank withdrawal happens
  • Process a high number of credit card validation requests
  • Ensure that a credit card transaction is not processed more than once
  • Schedule multiple entries to be added to a database that are not order dependent
  • Prevent a student from enrolling in a course before they register for an account

18 of 74

Amazon SQS architecture example

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

18

AWS Cloud

Auto Scaling group

EC2 instances

User

S3 bucket

SQS queue

Lambda function

Sends message about image to SQS queue

Processes image and sends back to S3 bucket

Polls SQS queue

Invokes Lambda function

Uploads photo to S3 bucket

19 of 74

Section 4: Working with Amazon SQS messages

Module 10: Developing with Messaging Services

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

20 of 74

Amazon SQS message lifecycle

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

20

Producer 1

Consumer 1

A

SQS queue �(distributed on SQS servers)

A

A

A

SendMessageProducer delivers message(s) to a queue

Amazon SQS redundantly stores the message across multiple Amazon SQS servers.

ReceiveMessageConsumer polls the queue and retrieves messages

Amazon SQS sets the visibility timeout on the retrieved message(s) in the queue

DeleteMessageAfter processing the message successfully, the consumer deletes it from the queue

1

2

3

Producer 2

B

A

B

B

B

B

Consumer 2

21 of 74

The SendMessage operation

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

21

Producer

aws sqs send-message --queue-url https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue --message-body "My first message"

{ �"MD5OfMessageBody": "51b0a325...39163aa0", �"MessageId": "d6790f8d-d575-4f01-bc51-40122EXAMPLE" �}

SQS queue

Example Amazon SQS response with MD5 hash of body and MessageID

Example AWS CLI command for SendMessage operation

22 of 74

The ReceiveMessage operation

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

22

aws sqs receive-message

--queue-url https://sqs.us-east1.amazonaws.com/80398EXAMPLE/MyQueue

--max-number-of-messages 10

"Messages": [

{

"Body": "My first message.",

"ReceiptHandle": "AQEBzbVv...fqNzFw==",

"MD5OfBody": "51b0a325...39163aa0",

"MessageId": "d6790f8d-d575-4f01-bc51-40122EXAMPLE",

"Attributes": {

},

}

]

}

Consumer

Example AWS CLI command for ReceiveMessage operation

Example Amazon SQS response with messages. Each message has a ReceiptHandle.

SQS queue

23 of 74

ReceiveMessage and visibility timeout

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

23

SQS queue

A

Time

ReceiveMessage�Consumer 1

Message A �returned

Message A �returned

Message A not�returned

ReceiveMessage�Consumer 2

ReceiveMessage�Consumer 3

ReceiveMessage�Consumer 4

Visibility timeout (default = 30 seconds)

A

A

A

A

A

Consumer 1 should process and delete message A from the queue before it becomes visible again

Message A not�returned

A

A

A

A

A

24 of 74

Configuring a dead-letter queue

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

24

Source SQS queue

A

Time

ReceiveMessage�

ReceiveMessage�

Visibility timeout

A

A

A

A

1

2

A

Visibility timeout

A

Consumer fails to complete processing

A

Consumer fails to complete processing

Dead-letter queue

A

A

A

3

Producer 1

A

A

maxReceiveCount= 2

25 of 74

ReceiveMessage and short polling

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

25

SQS queue

(distributed on SQS servers)

A

D

E

A

C

D

B

Messages

received from

sampled servers

ReceiveMessage request with WaitTimeSeconds = 0

B

C

E

E

C

D

A

C

A

E

B

B

C

D

D

A

B

Amazon SQS samples the servers

Consumer 1

Consumer 2

26 of 74

ReceiveMessage and long polling

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

26

SQS queue

(distributed on SQS servers)

A

D

E

A

C

D

B

Messages

Received from all servers

ReceiveMessage request with WaitTimeSeconds > 0

B

C

E

E

C

D

A

C

D

A

B

Amazon SQS queries all servers

E

A

E

B

B

C

D

Consumer 1

Consumer 2

27 of 74

ReceiveMessage long polling example

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

27

https://sqs.us-east-1.amazonaws.com/123456789012/testQueue/

?Action=ReceiveMessage

&WaitTimeSeconds=10

&MaxNumberOfMessages=5

&VisibilityTimeout=15

&AttributeName=All;

&Expires=2019-04-18T22%3A52%3A43PST

&Version=2012-11-05

&AUTPARAMS

Retrieve messages from "testQueue" at this URL

Use long polling with a wait time of 10 seconds

Retrieve up to 5 messages at a time

Make these messages invisible to other consumers for 15 seconds

28 of 74

DeleteMessage operation

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

28

aws sqs delete-message

--queue-url https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue

--receipt-handle AQEBRXTo...q2doVA==

Consumer

Example AWS CLI command for DeleteMessage operation

SQS queue

Amazon SQS automatically deletes messages that remain in queue beyond the queue's retention period.

Amazon SQS deletes the message with the specified receipt handle. If you use the DeleteMessageBatch option, Amazon SQS responds with a list of successes and failures.

AQEBRXTo...q2doVA==

29 of 74

Amazon SQS message lifecycle operations summary

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

29

Producer 1

Consumer 1

A

SQS queue �(distributed on SQS servers)

A

aws sqs send-message --queue-url https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue --message-body "My first message"

aws sqs receive-message

--queue-url https://sqs.us-east1.amazonaws.com/80398EXAMPLE/MyQueue

--max-number-of-messages 1

1

2

3

Producer 2

B

A

B

B

Consumer 2

aws sqs delete-message

--queue-url https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue

--receipt-handle AQEBRXTo...q2doVA==

30 of 74

Section 4 key takeaways

  • Developers use the SendMessage, ReceiveMessage, and DeleteMessage API operations to add, retrieve, and delete queue messages.
  • Amazon SQS makes a retrieved message invisible on the queue for the duration of the visibility timeout.
  • Amazon SQS can send failed records to a dead-letter queue for separate processing.
  • Short polling samples Amazon SQS servers for messages, while long polling queries all servers.

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

30

31 of 74

Section 5: Configuring Amazon SQS queues

Module 10: Developing with Messaging Services

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

32 of 74

Standard SQS queues

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

32

Duplicates are possible

Messaging order is not guaranteed

Nearly unlimited throughput

Producer 1

Consumer 1

A

SQS queue �(distributed on SQS servers)

A

A

A

A

B

B

B

B

Consumer 2

B

C

C

C

C

C

A

C

B

C

App database

33 of 74

FIFO SQS queues

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

33

Messaging order is preserved

Producer 1

Consumer 1

A

SQS queue �(distributed on SQS servers)

A

A

A

A

B

B

B

B

Consumer 2

B

C

C

C

C

C

A

B

C

App database

More limited throughput

Messages are delivered exactly once

34 of 74

Creating and configuring queues

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

34

API operation

Description

CreateQueue

Creates an SQS queue including queue attributes. If you don't provide a value for an attribute, the queue is created with the default value for the attribute.

GetQueueAttributes

Retrieves attributes of a queue including those that help you estimate the resources required to process queue messages; for example, the approximate number of messages available to retrieve and how many are currently invisible.

SetQueueAttributes

Sets attributes on an existing queue.

GetQueueUrl

Retrieves the URL of an SQS queue.

ListQueues

Retrieves a list of your queues.

DeleteQueue

Deletes a queue, regardless of whether the queue is empty. When a queue is deleted, any messages in the queue are no longer available.

35 of 74

Create SQS queue example

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

35

aws sqs create-queue --queue-name MyQueue --attributes file://create-queue.json

Create a queue using the attributes in create-queue.json

create-queue.json

{

"RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:us-east-1:80398EXAMPLE:MyDeadLetterQueue\",\"maxReceiveCount\":\"1000\"}",

"MessageRetentionPeriod": "259200"

}

Set the dead-letter queue destination

Set the message retention period to 3 days

Set the maximum receive count to 1,000

36 of 74

Three types of SQS queue security

AWS Identity and Access Management (IAM) and Amazon SQS policies

Server-side encryption (SSE) with AWS Key Management Service (AWS KMS)

Amazon Virtual Private Cloud (Amazon VPC) endpoint

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

36

Data encryption

Internetwork traffic privacy

Identity and access management

37 of 74

Examples of IAM and Amazon SQS policies

IAM policy attached to IAM user Bob

Allow

Actions:

ReceiveMessage, SendMessage

Resource:

arn:aws:sqs:us-east-1:80398EXAMPLE:MyQueue

Amazon SQS policy attached to MyQueue

Allow who:

Bob

Actions:

ReceiveMessage, SendMessage

Resource:

arn:aws:sqs:us-east-1:80398EXAMPLE:MyQueue

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

37

Equivalent permissions to MyQueue

38 of 74

Section 5 key takeaways

  • Standard queues provide nearly unlimited throughput but don't preserve message order and might include duplicate messages.
  • FIFO queues preserve order and provide exactly-once delivery but have more limited throughput.
  • Queue security includes managing access to queue resources, protecting data stored in the queue, and limiting exposure of messages to the internet.

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

38

39 of 74

Section 6: Introducing Amazon SNS

Module 10: Developing with Messaging Services

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

40 of 74

Amazon SNS

Fully managed messaging service with pub/sub functionality for many-to-many messaging between distributed systems, microservices, and event-driven applications

  • Offloads message filtering logic from your subscriber systems and message routing logic from your publisher systems
  • Reliably delivers messages and provides failure management features (retries, dead-letter queues)
  • Scales dynamically
  • Offers a FIFO topics option, which works with FIFO SQS queues to preserve message order

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

40

Amazon Simple Notification Service (Amazon SNS)

41 of 74

Amazon SNS overview

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

41

SNS topic

Publisher

Subscribers

Endpoint types

Application-to-application messaging (A2A)

Lambda functions

SQS queues

Kinesis Data Firehose

HTTP(S)

Application-to-person messaging (A2P)

Email

Text messaging (SMS)

Mobile push notifications

Publisher

Publisher

A topic acts as the logical access point

All messages are pushed to all subscriber endpoints

Publishers send messages�to a topic

42 of 74

Amazon SNS fanout pattern example

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

42

Subscribers

Publisher

SNS topic

“new-order”

Ordering system

Data warehouse SQS queue

EC2 instance

Fulfillment SQS queue

EC2�instance

“new order-ABCD”

message

Order fulfillment application

Data warehouse application

DEV queue

Lambda function

Dev database

43 of 74

Image processing fanout example

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

43

Source �S3 bucket

Destination �S3 bucket

User

SNS topic

SQS queue�Generate thumbnail

SQS queue�Size image for mobile

SQS queue�Size image for web

Lambda�functions

44 of 74

Section 7: Developing with Amazon SNS

Module 10: Developing with Messaging Services

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

45 of 74

Amazon SNS message delivery lifecycle

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

45

SNS topic

Unavailable subscriber endpoint

A

A

Publisher

A

Return MessageID

Failed delivery

A

Retry per delivery policy

Dead-letter queue

A

Discard or send to dead-letter queue when retries are exhausted

46 of 74

Amazon SNS API operations

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

46

API operation

Description

Input

Output

CreateTopic

Creates a topic

  • Topic name
  • ARN of topic

Subscribe

Prepares to subscribe to an endpoint

  • Subscriber’s endpoint
  • Protocol
  • ARN of topic
  • ARN of topic

ConfirmSubscription

Verifies an endpoint owner's intent to receive messages

  • Token sent to endpoint

Publish

Sends a message to all of a topic's subscribed endpoints

  • Message
  • Message attributes (optional)
  • Message structure:json (optional)
  • Subject (optional)
  • ARN of topic
  • Message ID

DeleteTopic

Deletes a topic and all of its subscriptions

  • ARN of topic

47 of 74

Amazon SNS message filtering

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

47

Subscribers with filter policies

Ecommerce

website

Online buyer

SNS topic

Shopping

Events

SQS queue

Payment

Lambda

function�SearchIndex

Payment

gateway

Search

engine

event_type=

order_cancelled

event_type=

order_placed

event_type=

product_page_visited

Filter: �"event_type": [{"prefix": "order"}]

Filter:

"event_type": ["product_page_visited"]

48 of 74

Subscription filter policy example

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

48

topic_arn = sns.create_topic(

Name=‘ShoppingEvents’

)['TopicArn'}

search_engine_subscription_arn = sns.subscribe(

TopicArn = topic_arn,

Protocol = 'lambda',

Endpoint = 'arn:aws:lambda:us-east-1:123456789012:function:SearchIndex'

)['SubscriptionArn']

sns.set_subscription_attributes(

SubscriptionArn = search_engine_subscription_arn,

AttributeName = 'FilterPolicy'

AttributeValue = '{"event_type": ["product_page_visited"]}'

)

SNS topic

Attribute in filter policy

Subscriber

49 of 74

Message with attributes example

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

49

message = '{"product": {"id": 1251, "status": "in_stock"},'\

' "buyer": {"id":4454}}'

sns.publish(

TopicArn = topic_arn,

Subject = "Product Visited #1251",

Message = message,

MessageAttributes = {

'event_type': {

'DataType': 'String',

'StringValue': 'product_page_visited'

}

}

}

Message

Message attribute that matches the attribute in the filter policy

50 of 74

Three types of Amazon SNS security

IAM and Amazon SNS policies

Server-side encryption (SSE) with AWS KMS

Amazon VPC endpoint

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

50

Data encryption

Internetwork traffic privacy

Identity and access management

51 of 74

Demonstration: Working with Amazon Messaging Services

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

51

52 of 74

Sections 6 and 7 key takeaways

  • Developers publish messages to SNS topics, and subscribers to that topic get copies of all published messages.
  • With attribute filtering, subscribers receive only relevant messages.
  • Subscriber endpoints include both application-to-application (A2A) and application-to-person (A2P) types.
  • Amazon SNS security includes managing access to SNS resources, protecting data sent to topics, and limiting exposure of messages to the internet.

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

52

53 of 74

Section 8: Introducing Kinesis Data Streams

Module 10: Developing with Messaging Services

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

54 of 74

Kinesis Data Streams

  • Continuously capture gigabytes of data per second
  • Source stream data from sources including website clickstreams, database event streams, financial transactions, social media feeds, IT logs, and location-tracking events
  • Easily process data with built-in integrations with Lambda and other Kinesis services such as Kinesis Data Analytics and Kinesis Data Firehose

Fully managed, massively scalable and durable real-time data streaming service that you can use to ingest, buffer, and process streaming data in real time

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

54

Amazon Kinesis�Data Streams

55 of 74

Kinesis Data Streams use cases

Log and event data collection

Real-time analytics

Mobile data capture

Gaming data feed

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

55

56 of 74

Kinesis Data Streams overview

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

56

Kinesis data stream

Producer

Consumers

Producer

Producer

Put messages on the stream

The stream stores data durably distributed on shards

Custom application written with KCL deployed on Amazon EC2

Lambda/�Lambda function

Kinesis Data Firehose stream connected to a destination like Amazon S3

Kinesis Data Analytics application connected to another stream or a Lambda function

Poll the stream

Shard 1

Shard 2

Records are available during the retention period

57 of 74

Other Kinesis data streaming services

Amazon Kinesis Data Firehose

  • Deliver streaming data to data stores without writing consumer applications for your stream
  • Automatically convert incoming data to open and standards based before the data is delivered

Amazon Kinesis Data Analytics

  • Perform real-time analysis on data in the stream using SQL queries before persisting the data
  • Use Apache Flink, Java, Scala, or Python for your analysis application

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

57

58 of 74

Kinesis Data Streams example

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

58

Amazon Kinesis

Data Streams

Amazon Kinesis

Data Firehose

Amazon Simple Storage Service (Amazon S3)

Amazon Elastic Compute Cloud (Amazon EC2)

AWS Lambda

Amazon Elastic Container Service

(Amazon ECS)

Amazon Kinesis

Data Analytics

Amazon Elasticsearch Service (Amazon ES)

Amazon Athena

Amazon CloudWatch

59 of 74

Kinesis API operations

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

59

Kinesis data stream

Producers

Consumers

Shard 1

Shard 2

CreateStream: Creates a stream. Requires ShardCount and StreamName.

DeleteStream: Deletes the named stream and all of its data shards.

DescribeStreamSummary: Provides summary information about a named stream.

UpdateShardcount: Modifies the number of shards on the stream.

MergeShards: Merges two shards to reduce the stream's capacity.

SplitShards: Splits a shard into two shards to increase the stream's capacity.

PutRecord/PutRecords: Writes a record or records to the stream. Includes a partition key that determines which shard the record(s) will be written to.

GetShardIterator: Gets the shard iterator, which tells you the shard position from which to start reading records.

GetRecords: Reads records from the stream, starting from a specified shard iterator.

60 of 74

Examples using the AWS CLI�for basic tasks

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

60

aws kinesis create-stream --stream-name MyStream --shard-count 1

aws kinesis describe-stream-summary --stream-name MyStream

aws kinesis put-record --stream-name MyStream --partition-key 123 --data testdata

Create a stream named MyStream with one shard

Check whether your new stream is ready to be used

Put a test record on the stream

Get the starting position to read the stream, then use the output value to get records

aws kinesis get-shard-iterator --shard-id shardId-000000000000 --shard-iterator-type TRIM_HORIZON --stream-name MyStream

aws kinesis get-records --shard-iterator AAAAAAAAAAHSywl…

61 of 74

Building producers and consumers

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

61

Feature

Kinesis Producer Library (KPL)

Kinesis Client Library (KCL)

Kinesis API with AWS SDK

Abstracts stream management tasks so you can focus on application logic

Handles producer tasks such as writing with automatic retries and aggregating records to optimize throughput

Handles consumer subtasks such as load balancing and checkpointing processed records

Provides the ability to write producer or consumer applications using direct interaction with the Kinesis API

Requires your application code to handle stream management

62 of 74

Comparison of queues and streams

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

62

Queues

Streams

Data value

The value comes from processing individual messages.

The value comes from aggregating messages to get actionable data.

Message rate

The message rate is variable.

The message rate is continuous and high volume.

Message processing

Messages are deleted after a consumer successfully processes them.

Messages are available to multiple consumers to process in parallel, and each consumer maintains a pointer but does not delete records.

Example use cases

Financial transactions, product orders

Clickstream data, application logs

63 of 74

Section 8 key takeaways

  • With Kinesis Data Streams, you can ingest, buffer, and process streaming data in real time.
  • Kinesis Data Firehose and Kinesis Data Analytics were designed to simplify common data streaming use cases.
  • Producers put records on to a stream where the records are stored in shards, and consumers get records off the stream for processing.
  • The Kinesis Producer Library (KPL) and Kinesis Client Library (KCL) abstract stream interactions for developers.

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

63

64 of 74

Lab 10.1: �Implementing a Messaging System Using Amazon SNS and Amazon SQS��

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

64

65 of 74

Lab: Tasks

  1. Preparing the development environment
  2. Configuring the Amazon SQS dead-letter queue
  3. Configuring the Amazon SQS queue
  4. Configuring the Amazon SNS topic
  5. Linking Amazon SQS and Amazon SNS
  6. Testing message publishing
  7. Configuring the application to poll the queue

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

65

66 of 74

Lab: Final product

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

66

Elastic Beanstalk deployment

Coffee suppliers application

Aurora Serverless

database

Coffee suppliers

send inventory updates

Amazon SNS�topic

Amazon SQS�queue

Amazon SQS�dead-letter queue

67 of 74

Begin Lab 10.1: Implementing a Messaging System Using Amazon SNS and Amazon SQS

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

67

~ 90 minutes

68 of 74

Lab debrief: �Key takeaways

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

68

69 of 74

Module wrap-up

Module 10: Developing with Messaging Services

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

70 of 74

Module summary

In summary, in this module, you learned how to do the following:

  • Illustrate how messaging services, including queues, pub/sub messaging, and streams, support asynchronous processing
  • Describe Amazon SQS
  • Send messages to an SQS queue
  • Describe Amazon SNS
  • Subscribe an SQS queue to an SNS topic
  • Describe how Kinesis can be used for real-time analytics

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

70

71 of 74

Complete the knowledge check

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

71

72 of 74

Sample exam question

A developer wants to introduce an asynchronous connection in their application workflow that processes individual banking transactions including deposits and withdrawals. Transactions must be handled in the order they arrive. Which method would meet the developer's needs?

  1. Send transaction messages to a standard SQS queue.
  2. Put transaction messages on to a Kinesis data stream.
  3. Send transaction messages to a FIFO SQS queue.
  4. Put transactions on to a Kinesis Data Firehose delivery stream.

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

72

73 of 74

Thank you

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. This work may not be reproduced or redistributed, in whole or in part, without prior written permission from Amazon Web Services, Inc. Commercial copying, lending, or selling is prohibited. Corrections, feedback, or other questions? Contact us at https://support.aws.amazon.com/#/contacts/aws-training. All trademarks are the property of their owners.

74 of 74

Messaging services as part of developing a cloud application

© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

74

Step Functions �retrieves data from RDS and posts report to S3 with presigned URL

Amazon Cognito provides token for report requests

Amazon S3�

Developers

AWS Cloud9

CloudShell

AWS CLI

SDK for Python

Console

�bucket hosts café website

Café website users

Webpage �requests

Bucket policy

DynamoDB database stores products table

API Gateway REST API

Lambda function �does database lookups

Café employees

Elastic Beanstalk

ECR hosts Docker image

EC2 instance with Docker container runs coffee supplier website

Aurora Serverless on RDS stores supplier database

AWS WAF �secures API endpoint

Café employees

Refresh cache per settings

CloudFront distribution

ElastiCache �for Memcached

AWS WAF �secures website

SNS

SQS

Suppliers

Coffee inventory

updates

Lambda