ExAws.SQS (ExAws.SQS v4.0.0)

View Source

Operations on AWS SQS.

This is a modernized fork of the archived ex-aws/ex_aws_sqs. The biggest change is that every operation now speaks the SQS JSON protocol (AmazonSQS.<Action> over application/x-amz-json-1.0) instead of the legacy Query/XML protocol. See the README for the full list of changes and a migration guide.

Responses

Because requests are built with ExAws.Operation.JSON, a successful ExAws.request/1 call returns {:ok, response} where response is the JSON response body decoded as-is by your configured :json_codec (Elixir's built-in JSON module since 1.18, or Jason, etc.) — a map with the exact field names AWS documents, e.g. %{"MessageId" => "...", "MD5OfMessageBody" => "..."} for send_message/3. There is no extra normalization layer: whatever the AWS API Reference says a call returns is what you get back, keyed exactly as documented (a handful of AWS operations use unusual casing, e.g. list_dead_letter_source_queues/1 returns "queueUrls" with a lowercase q — that's an AWS quirk, not a bug here).

Summary

Functions

Adds a permission with the provided label to the Queue for a specific action for a specific account.

Cancels a specified message movement task.

Extends the read lock timeout for the specified message from the specified queue to the specified value.

Extends the read lock timeout for a batch of 1..10 messages.

Delete a message from a SQS Queue

Deletes a list of messages from a SQS Queue in a single request

Delete a queue

Retrieves the dead letter source queues for a given SQS Queue

Gets the most recent message movement tasks (up to the last 31 days) for a specified source queue ARN.

List tags of a SQS Queue.

Retrieves a list of all the SQS Queues

Purge all messages in a SQS Queue

Read messages from a SQS Queue

Removes permission with the given label from the Queue

Send a message to a SQS Queue

Send up to 10 messages to a SQS Queue in a single request.

Set attributes of a SQS Queue.

Starts an asynchronous task to move messages from a specified source queue to a specified destination queue.

Apply tags to a SQS Queue.

Remove tags from a SQS Queue.

Types

delete_message_batch_item()

@type delete_message_batch_item() :: %{id: binary(), receipt_handle: binary()}

message_visibility_batch_item()

@type message_visibility_batch_item() :: %{
  id: binary(),
  receipt_handle: binary(),
  visibility_timeout: visibility_timeout()
}

queue_attributes()

@type queue_attributes() :: [
  policy: binary(),
  visibility_timeout: visibility_timeout(),
  maximum_message_size: 1024..262_144,
  message_retention_period: 60..1_209_600,
  approximate_number_of_messages: binary(),
  approximate_number_of_messages_not_visible: binary(),
  created_timestamp: binary(),
  last_modified_timestamp: binary(),
  queue_arn: binary(),
  approximate_number_of_messages_delayed: binary(),
  delay_seconds: 0..900,
  receive_message_wait_time_seconds: 0..20,
  redrive_policy: binary(),
  redrive_allow_policy: binary(),
  fifo_queue: boolean(),
  content_based_deduplication: boolean(),
  kms_master_key_id: binary(),
  kms_data_key_reuse_period_seconds: 60..86400,
  deduplication_scope: binary(),
  fifo_throughput_limit: binary(),
  sqs_managed_sse_enabled: boolean()
]

receive_message_opts()

@type receive_message_opts() :: [
  attribute_names: :all | [sqs_message_attribute_name(), ...],
  message_attribute_names: :all | [String.Chars.t(), ...],
  max_number_of_messages: 1..10,
  visibility_timeout: 0..43200,
  wait_time_seconds: 0..20,
  receive_request_attempt_id: String.t()
]

sqs_acl()

@type sqs_acl() :: %{required(binary()) => :all | [sqs_permission(), ...]}

sqs_batch_message()

@type sqs_batch_message() ::
  map()
  | [
      id: binary(),
      message_body: binary(),
      delay_seconds: 0..900,
      message_attributes:
        sqs_message_attribute() | [sqs_message_attribute(), ...],
      message_deduplication_id: binary(),
      message_group_id: binary()
    ]

sqs_message_attribute()

@type sqs_message_attribute() :: %{
  :name => binary(),
  :data_type => :string | :binary | :number,
  optional(:custom_type) => binary(),
  value: binary() | number()
}

sqs_message_attribute_name()

@type sqs_message_attribute_name() ::
  :sender_id
  | :sent_timestamp
  | :approximate_receive_count
  | :approximate_first_receive_timestamp
  | :sequence_number
  | :message_deduplication_id
  | :message_group_id
  | :aws_trace_header
  | :dead_letter_queue_source_arn

sqs_message_opts()

@type sqs_message_opts() :: [
  delay_seconds: 0..900,
  message_attributes: sqs_message_attribute() | [sqs_message_attribute(), ...],
  message_deduplication_id: binary(),
  message_group_id: binary()
]

sqs_permission()

@type sqs_permission() ::
  :send_message
  | :receive_message
  | :delete_message
  | :change_message_visibility
  | :get_queue_attributes

sqs_queue_attribute_name()

@type sqs_queue_attribute_name() ::
  :policy
  | :visibility_timeout
  | :maximum_message_size
  | :message_retention_period
  | :approximate_number_of_messages
  | :approximate_number_of_messages_not_visible
  | :created_timestamp
  | :last_modified_timestamp
  | :queue_arn
  | :approximate_number_of_messages_delayed
  | :delay_seconds
  | :receive_message_wait_time_seconds
  | :redrive_policy
  | :redrive_allow_policy
  | :fifo_queue
  | :content_based_deduplication
  | :deduplication_scope
  | :fifo_throughput_limit
  | :sqs_managed_sse_enabled

visibility_timeout()

@type visibility_timeout() :: 0..43200

Functions

add_permission(queue_url, label, permissions \\ %{})

@spec add_permission(
  queue_url :: binary(),
  label :: binary(),
  permissions :: sqs_acl()
) ::
  ExAws.Operation.JSON.t()

Adds a permission with the provided label to the Queue for a specific action for a specific account.

AWS API Docs

cancel_message_move_task(task_handle)

@spec cancel_message_move_task(task_handle :: binary()) :: ExAws.Operation.JSON.t()

Cancels a specified message movement task.

AWS API Docs

change_message_visibility(queue_url, receipt_handle, visibility_timeout)

@spec change_message_visibility(
  queue_url :: binary(),
  receipt_handle :: binary(),
  visibility_timeout :: visibility_timeout()
) :: ExAws.Operation.JSON.t()

Extends the read lock timeout for the specified message from the specified queue to the specified value.

AWS API Docs

change_message_visibility_batch(queue_url, messages)

@spec change_message_visibility_batch(
  queue_url :: binary(),
  opts :: [message_visibility_batch_item(), ...]
) :: ExAws.Operation.JSON.t()

Extends the read lock timeout for a batch of 1..10 messages.

AWS API Docs

create_queue(queue_name, attributes \\ [], tags \\ %{})

@spec create_queue(
  queue_name :: binary(),
  queue_attributes :: queue_attributes(),
  tags :: map()
) ::
  ExAws.Operation.JSON.t()

Create queue.

AWS API Docs

Attributes

  • :delay_seconds - The length of time, in seconds, for which the delivery of all messages in the queue is delayed. Valid values: An integer from 0 to 900 seconds (15 minutes). Default: 0.

  • :maximum_message_size - The limit of how many bytes a message can contain before Amazon SQS rejects it. Valid values: An integer from 1,024 bytes (1 KiB) to 262,144 bytes (256 KiB). Default: 262,144 (256 KiB).

  • :message_retention_period - The length of time, in seconds, for which Amazon SQS retains a message. Valid values: An integer from 60 seconds (1 minute) to 1,209,600 seconds (14 days). Default: 345,600 (4 days).

  • :policy - The queue's policy. A valid AWS policy. For more information about policy structure, see Overview of AWS IAM Policies in the Amazon IAM User Guide.

  • :receive_message_wait_time_seconds - The length of time, in seconds, for which a ReceiveMessage action waits for a message to arrive. Valid values: An integer from 0 to 20 (seconds). Default: 0.

  • :redrive_policy - The string that includes the parameters for the dead-letter queue functionality of the source queue as a JSON object. For more information about the redrive policy and dead-letter queues, see Using Amazon SQS Dead-Letter Queues in the Amazon Simple Queue Service Developer Guide.

    • deadLetterTargetArn – The Amazon Resource Name (ARN) of the dead-letter queue to which Amazon SQS moves messages after the value of maxReceiveCount is exceeded.

    • maxReceiveCount – The number of times a message is delivered to the source queue before being moved to the dead-letter queue. When the ReceiveCount for a message exceeds the maxReceiveCount for a queue, Amazon SQS moves the message to the dead-letter-queue.

    Note

    The dead-letter queue of a FIFO queue must also be a FIFO queue. Similarly, the dead-letter queue of a standard queue must also be a standard queue.

  • :visibility_timeout - The visibility timeout for the queue, in seconds. Valid values: An integer from 0 to 43,200 (12 hours). Default: 30. For more information about the visibility timeout, see Visibility Timeout in the Amazon Simple Queue Service Developer Guide.

  • :fifo_queue - Designates a queue as FIFO. Valid values: true, false. If you don't specify the FifoQueue attribute, Amazon SQS creates a standard queue. You can provide this attribute only during queue creation. You can't change it for an existing queue. When you set this attribute, you must also provide the MessageGroupId for your messages explicitly. For more information, see FIFO Queue Logic in the Amazon Simple Queue Service Developer Guide.

  • :content_based_deduplication - Enables content-based deduplication. Valid values: true, false. For more information, see Exactly-Once Processing in the Amazon Simple Queue Service Developer Guide.

  • :kms_master_key_id - The ID of an AWS-managed customer master key (CMK) for Amazon SQS or a custom CMK. For more information, see Key Terms. While the alias of the AWS-managed CMK for Amazon SQS is always alias/aws/sqs, the alias of a custom CMK can, for example, be alias/MyAlias . For more examples, see KeyId in the AWS Key Management Service API Reference.

  • :kms_data_key_reuse_period_seconds - The length of time, in seconds, for which Amazon SQS can reuse a data key to encrypt or decrypt messages before calling AWS KMS again. An integer representing seconds, between 60 seconds (1 minute) and 86,400 seconds (24 hours). Default: 300 (5 minutes).

  • :sqs_managed_sse_enabled - Enables server-side queue encryption using SQS owned encryption keys. Valid values: true, false.

Examples

iex> ExAws.SQS.create_queue("my-queue", [visibility_timeout: 60], %{"team" => "platform"}).data
%{
  "QueueName" => "my-queue",
  "Attributes" => %{"VisibilityTimeout" => "60"},
  "tags" => %{"team" => "platform"}
}

delete_message(queue_url, receipt_handle)

@spec delete_message(queue_url :: binary(), receipt_handle :: binary()) ::
  ExAws.Operation.JSON.t()

Delete a message from a SQS Queue

AWS API Docs

delete_message_batch(queue_url, messages)

@spec delete_message_batch(
  queue_url :: binary(),
  message_receipts :: [delete_message_batch_item(), ...]
) :: ExAws.Operation.JSON.t()

Deletes a list of messages from a SQS Queue in a single request

AWS API Docs

delete_queue(queue_url)

@spec delete_queue(queue_url :: binary()) :: ExAws.Operation.JSON.t()

Delete a queue

AWS API Docs

get_queue_attributes(queue_url, attributes \\ :all)

@spec get_queue_attributes(
  queue_url :: binary(),
  attribute_names :: :all | [sqs_queue_attribute_name(), ...]
) :: ExAws.Operation.JSON.t()

Gets attributes of a SQS Queue

AWS API Docs

get_queue_url(queue_name, opts \\ [])

@spec get_queue_url(
  queue_name :: binary(),
  opts :: [{:queue_owner_aws_account_id, binary()}]
) ::
  ExAws.Operation.JSON.t()

Get queue URL

AWS API Docs

Options

  • :queue_owner_aws_account_id - The AWS account ID of the account that created the queue.

list_dead_letter_source_queues(queue_url, opts \\ [])

@spec list_dead_letter_source_queues(
  queue_url :: binary(),
  opts :: [max_results: pos_integer(), next_token: binary()]
) :: ExAws.Operation.JSON.t()

Retrieves the dead letter source queues for a given SQS Queue

AWS API Docs

Options

  • :max_results - Maximum number of results to include in the response.
  • :next_token - Pagination token from a previous call, used to retrieve the next page of results.

list_message_move_tasks(source_arn, opts \\ [])

@spec list_message_move_tasks(source_arn :: binary(), opts :: [{:max_results, 1..10}]) ::
  ExAws.Operation.JSON.t()

Gets the most recent message movement tasks (up to the last 31 days) for a specified source queue ARN.

AWS API Docs

Options

  • :max_results - Maximum number of results to include in the response, from 1 to 10. Default 1.

list_queue_tags(queue_url)

@spec list_queue_tags(queue_url :: binary()) :: ExAws.Operation.JSON.t()

List tags of a SQS Queue.

AWS API Docs

list_queues(opts \\ [])

@spec list_queues(
  opts :: [
    queue_name_prefix: binary(),
    max_results: pos_integer(),
    next_token: binary()
  ]
) :: ExAws.Operation.JSON.t()

Retrieves a list of all the SQS Queues

AWS API Docs

Options

  • :queue_name_prefix - A string to use for filtering the list results. Only those queues whose name begins with the specified string are returned. Queue URLs and names are case-sensitive.
  • :max_results - Maximum number of results to include in the response.
  • :next_token - Pagination token from a previous call, used to retrieve the next page of results.

purge_queue(queue_url)

@spec purge_queue(queue_url :: binary()) :: ExAws.Operation.JSON.t()

Purge all messages in a SQS Queue

AWS API Docs

receive_message(queue_url, opts \\ [])

@spec receive_message(queue_url :: binary(), opts :: receive_message_opts()) ::
  ExAws.Operation.JSON.t()

Read messages from a SQS Queue

AWS API Docs

Options

  • :attribute_names - :all or a list of message system attributes to include in the response (sent as the modern MessageSystemAttributeNames field). Valid attributes are: [:sender_id, :sent_timestamp, :approximate_receive_count, :approximate_first_receive_timestamp, :sequence_number, :message_deduplication_id, :message_group_id, :aws_trace_header, :dead_letter_queue_source_arn]

  • :message_attribute_names - :all or a list of message attributes to include.

    • The name can contain alphanumeric characters and the underscore (_), hyphen (-), and period (.).

    • The name is case-sensitive and must be unique among all attribute names for the message.

    • The name must not start with AWS-reserved prefixes such as AWS. or Amazon. (or any casing variants).

    • The name must not start or end with a period (.), and it should not have periods in succession (..).

    • The name can be up to 256 characters long.

  • :max_number_of_messages - The maximum number of messages to return. Amazon SQS never returns more messages than this value (however, fewer messages might be returned). Valid values: 1 to 10. Default: 1.

  • :visibility_timeout - The duration (in seconds) that the received messages are hidden from subsequent retrieve requests after being retrieved by a ReceiveMessage request.

  • :wait_time_seconds - The duration (in seconds) for which the call waits for a message to arrive in the queue before returning.

  • :receive_request_attempt_id - This parameter applies only to FIFO (first-in-first-out) queues. The token used for deduplication of ReceiveMessage calls.

remove_permission(queue_url, label)

@spec remove_permission(queue_url :: binary(), label :: binary()) ::
  ExAws.Operation.JSON.t()

Removes permission with the given label from the Queue

AWS API Docs

send_message(queue_url, message, opts \\ [])

@spec send_message(
  queue_url :: binary(),
  message_body :: binary(),
  opts :: sqs_message_opts()
) ::
  ExAws.Operation.JSON.t()

Send a message to a SQS Queue

AWS API Docs

Options

  • :delay_seconds - The length of time, in seconds, for which to delay a specific message. Valid values: 0 to 900.

  • :message_attributes - Each message attribute consists of a :name, :data_type, and :value. For binary attributes (data_type: :binary), pass the raw binary in :value; the library base64-encodes it for the JSON protocol. See Amazon SQS Message Attributes.

  • :message_deduplication_id - This parameter applies only to FIFO (first-in-first-out) queues.

  • :message_group_id - This parameter applies only to FIFO (first-in-first-out) queues.

Examples

iex> ExAws.SQS.send_message("https://queue.url", "Hello!").data
%{"QueueUrl" => "https://queue.url", "MessageBody" => "Hello!"}

iex> ExAws.SQS.send_message("https://queue.url", "Hello!",
...>   message_attributes: [%{name: "priority", data_type: :number, value: 1}]
...> ).data
%{
  "QueueUrl" => "https://queue.url",
  "MessageBody" => "Hello!",
  "MessageAttributes" => %{"priority" => %{"DataType" => "Number", "StringValue" => "1"}}
}

send_message_batch(queue_url, messages)

@spec send_message_batch(
  queue_url :: binary(),
  messages :: [sqs_batch_message(), ...]
) ::
  ExAws.Operation.JSON.t()

Send up to 10 messages to a SQS Queue in a single request.

Each entry needs at least an :id (unique within the batch, used to match up the "Successful"/"Failed" results in the response) and a :message_body. Entries may be keyword lists or maps.

AWS API Docs

Examples

iex> ExAws.SQS.send_message_batch("https://queue.url", [
...>   [id: "a1", message_body: "payload1"],
...>   [id: "a2", message_body: "payload2", delay_seconds: 10]
...> ]).data
%{
  "QueueUrl" => "https://queue.url",
  "Entries" => [
    %{"Id" => "a1", "MessageBody" => "payload1"},
    %{"Id" => "a2", "MessageBody" => "payload2", "DelaySeconds" => 10}
  ]
}

A successful call returns a body shaped like:

%{
  "Successful" => [%{"Id" => "a1", "MessageId" => "...", "MD5OfMessageBody" => "..."}, ...],
  "Failed" => [%{"Id" => "a2", "Code" => "...", "Message" => "...", "SenderFault" => true}, ...]
}

set_queue_attributes(queue_url, attributes \\ [])

@spec set_queue_attributes(queue_url :: binary(), attributes :: queue_attributes()) ::
  ExAws.Operation.JSON.t()

Set attributes of a SQS Queue.

AWS API Docs

start_message_move_task(source_arn, opts \\ [])

@spec start_message_move_task(
  source_arn :: binary(),
  opts :: [
    destination_arn: binary(),
    max_number_of_messages_per_second: pos_integer()
  ]
) :: ExAws.Operation.JSON.t()

Starts an asynchronous task to move messages from a specified source queue to a specified destination queue.

AWS API Docs

Options

  • :destination_arn - The ARN of the queue that receives the moved messages. Defaults to the dead-letter queue's redrive policy source queue.
  • :max_number_of_messages_per_second - Throughput cap for the move, from 1 to 500 messages per second.

Examples

iex> ExAws.SQS.start_message_move_task("arn:aws:sqs:us-east-1:123:MyDLQ").data
%{"SourceArn" => "arn:aws:sqs:us-east-1:123:MyDLQ"}

tag_queue(queue_url, tags)

@spec tag_queue(queue_url :: binary(), tags :: map()) :: ExAws.Operation.JSON.t()

Apply tags to a SQS Queue.

AWS API Docs

untag_queue(queue_url, tag_keys)

@spec untag_queue(queue_url :: binary(), tag_keys :: list()) ::
  ExAws.Operation.JSON.t()

Remove tags from a SQS Queue.

AWS API Docs