Class TopicSubscription


  • public class TopicSubscription
    extends com.pulumi.resources.CustomResource
    Provides a resource for subscribing to SNS topics. Requires that an SNS topic exist for the subscription to attach to. This resource allows you to automatically place messages sent to SNS topics in SQS queues, send them as HTTP(S) POST requests to a given endpoint, send SMS messages, or notify devices / applications. The most likely use case for provider users will probably be SQS queues. > **NOTE:** If the SNS topic and SQS queue are in different AWS regions, the `aws.sns.TopicSubscription` must use an AWS provider that is in the same region as the SNS topic. If the `aws.sns.TopicSubscription` uses a provider with a different region than the SNS topic, this provider will fail to create the subscription. > **NOTE:** Setup of cross-account subscriptions from SNS topics to SQS queues requires the provider to have access to BOTH accounts. > **NOTE:** If an SNS topic and SQS queue are in different AWS accounts but the same region, the `aws.sns.TopicSubscription` must use the AWS provider for the account with the SQS queue. If `aws.sns.TopicSubscription` uses a Provider with a different account than the SQS queue, this provider creates the subscription but does not keep state and tries to re-create the subscription at every `apply`. > **NOTE:** If an SNS topic and SQS queue are in different AWS accounts and different AWS regions, the subscription needs to be initiated from the account with the SQS queue but in the region of the SNS topic. > **NOTE:** You cannot unsubscribe to a subscription that is pending confirmation. If you use `email`, `email-json`, or `http`/`https` (without auto-confirmation enabled), until the subscription is confirmed (e.g., outside of this provider), AWS does not allow this provider to delete / unsubscribe the subscription. If you `destroy` an unconfirmed subscription, this provider will remove the subscription from its state but the subscription will still exist in AWS. However, if you delete an SNS topic, SNS [deletes all the subscriptions](https://docs.aws.amazon.com/sns/latest/dg/sns-delete-subscription-topic.html) associated with the topic. Also, you can import a subscription after confirmation and then have the capability to delete it. ## Example Usage ### Basic usage
     
     package generated_program;
     
     import com.pulumi.Context;
     import com.pulumi.Pulumi;
     import com.pulumi.core.Output;
     import com.pulumi.aws.sns.Topic;
     import com.pulumi.aws.sns.TopicArgs;
     import com.pulumi.aws.iam.IamFunctions;
     import com.pulumi.aws.iam.inputs.GetPolicyDocumentArgs;
     import com.pulumi.aws.iam.inputs.GetPolicyDocumentStatementArgs;
     import com.pulumi.aws.iam.inputs.GetPolicyDocumentStatementConditionArgs;
     import com.pulumi.aws.iam.inputs.GetPolicyDocumentStatementPrincipalArgs;
     import com.pulumi.aws.sqs.Queue;
     import com.pulumi.aws.sqs.QueueArgs;
     import com.pulumi.aws.sns.TopicSubscription;
     import com.pulumi.aws.sns.TopicSubscriptionArgs;
     import java.util.ArrayList;
     import java.util.Arrays;
     import java.util.Map;
     import java.io.File;
     import java.nio.file.Files;
     import java.nio.file.Paths;
     
     public class App {
         public static void main(String[] args) {
             Pulumi.run(App::stack);
         }
     
         public static void stack(Context ctx) {
             var userUpdates = new Topic("userUpdates", TopicArgs.builder()
                 .name("user-updates-topic")
                 .build());
     
             final var sqsQueuePolicy = IamFunctions.getPolicyDocument(GetPolicyDocumentArgs.builder()
                 .statements(GetPolicyDocumentStatementArgs.builder()
                     .conditions(GetPolicyDocumentStatementConditionArgs.builder()
                         .test("ArnEquals")
                         .variable("aws:SourceArn")
                         .values(userUpdates.arn())
                         .build())
                     .principals(GetPolicyDocumentStatementPrincipalArgs.builder()
                         .type("Service")
                         .identifiers("sns.amazonaws.com")
                         .build())
                     .sid("user_updates_sqs_target")
                     .effect("Allow")
                     .actions("SQS:SendMessage")
                     .resources("arn:aws:sqs:us-west-2:123456789012:user-updates-queue")
                     .build())
                 .policyId("arn:aws:sqs:us-west-2:123456789012:user_updates_queue/SQSDefaultPolicy")
                 .build());
     
             var userUpdatesQueue = new Queue("userUpdatesQueue", QueueArgs.builder()
                 .name("user-updates-queue")
                 .policy(sqsQueuePolicy.applyValue(_sqsQueuePolicy -> _sqsQueuePolicy.json()))
                 .build());
     
             var userUpdatesSqsTarget = new TopicSubscription("userUpdatesSqsTarget", TopicSubscriptionArgs.builder()
                 .topic(userUpdates.arn())
                 .protocol("sqs")
                 .endpoint(userUpdatesQueue.arn())
                 .build());
     
         }
     }
     
     
    ### Example Cross-account Subscription You can subscribe SNS topics to SQS queues in different Amazon accounts and regions:
     
     package generated_program;
     
     import com.pulumi.Context;
     import com.pulumi.Pulumi;
     import com.pulumi.core.Output;
     import com.pulumi.aws.iam.IamFunctions;
     import com.pulumi.aws.iam.inputs.GetPolicyDocumentArgs;
     import com.pulumi.aws.iam.inputs.GetPolicyDocumentStatementArgs;
     import com.pulumi.aws.iam.inputs.GetPolicyDocumentStatementConditionArgs;
     import com.pulumi.aws.iam.inputs.GetPolicyDocumentStatementPrincipalArgs;
     import com.pulumi.aws.sns.Topic;
     import com.pulumi.aws.sns.TopicArgs;
     import com.pulumi.aws.sqs.Queue;
     import com.pulumi.aws.sqs.QueueArgs;
     import com.pulumi.aws.sns.TopicSubscription;
     import com.pulumi.aws.sns.TopicSubscriptionArgs;
     import java.util.ArrayList;
     import java.util.Arrays;
     import java.util.Map;
     import java.io.File;
     import java.nio.file.Files;
     import java.nio.file.Paths;
     
     public class App {
         public static void main(String[] args) {
             Pulumi.run(App::stack);
         }
     
         public static void stack(Context ctx) {
             final var config = ctx.config();
             final var sns = config.get("sns").orElse(Map.ofEntries(
                 Map.entry("account-id", "111111111111"),
                 Map.entry("displayName", "example"),
                 Map.entry("name", "example-sns-topic"),
                 Map.entry("region", "us-west-1"),
                 Map.entry("role-name", "service/service")
             ));
             final var sqs = config.get("sqs").orElse(Map.ofEntries(
                 Map.entry("account-id", "222222222222"),
                 Map.entry("name", "example-sqs-queue"),
                 Map.entry("region", "us-east-1"),
                 Map.entry("role-name", "service/service")
             ));
             final var snsTopicPolicy = IamFunctions.getPolicyDocument(GetPolicyDocumentArgs.builder()
                 .statements(            
                     GetPolicyDocumentStatementArgs.builder()
                         .conditions(GetPolicyDocumentStatementConditionArgs.builder()
                             .test("StringEquals")
                             .variable("AWS:SourceOwner")
                             .values(sns.account-id())
                             .build())
                         .principals(GetPolicyDocumentStatementPrincipalArgs.builder()
                             .type("AWS")
                             .identifiers("*")
                             .build())
                         .actions(                    
                             "SNS:Subscribe",
                             "SNS:SetTopicAttributes",
                             "SNS:RemovePermission",
                             "SNS:Publish",
                             "SNS:ListSubscriptionsByTopic",
                             "SNS:GetTopicAttributes",
                             "SNS:DeleteTopic",
                             "SNS:AddPermission")
                         .effect("Allow")
                         .resources(String.format("arn:aws:sns:%s:%s:%s", sns.region(),sns.account-id(),sns.name()))
                         .sid("__default_statement_ID")
                         .build(),
                     GetPolicyDocumentStatementArgs.builder()
                         .conditions(GetPolicyDocumentStatementConditionArgs.builder()
                             .test("StringLike")
                             .variable("SNS:Endpoint")
                             .values(String.format("arn:aws:sqs:%s:%s:%s", sqs.region(),sqs.account-id(),sqs.name()))
                             .build())
                         .principals(GetPolicyDocumentStatementPrincipalArgs.builder()
                             .type("AWS")
                             .identifiers("*")
                             .build())
                         .actions(                    
                             "SNS:Subscribe",
                             "SNS:Receive")
                         .effect("Allow")
                         .resources(String.format("arn:aws:sns:%s:%s:%s", sns.region(),sns.account-id(),sns.name()))
                         .sid("__console_sub_0")
                         .build())
                 .policyId("__default_policy_ID")
                 .build());
     
             final var sqsQueuePolicy = IamFunctions.getPolicyDocument(GetPolicyDocumentArgs.builder()
                 .statements(GetPolicyDocumentStatementArgs.builder()
                     .conditions(GetPolicyDocumentStatementConditionArgs.builder()
                         .test("ArnEquals")
                         .variable("aws:SourceArn")
                         .values(String.format("arn:aws:sns:%s:%s:%s", sns.region(),sns.account-id(),sns.name()))
                         .build())
                     .principals(GetPolicyDocumentStatementPrincipalArgs.builder()
                         .type("AWS")
                         .identifiers("*")
                         .build())
                     .sid("example-sns-topic")
                     .effect("Allow")
                     .actions("SQS:SendMessage")
                     .resources(String.format("arn:aws:sqs:%s:%s:%s", sqs.region(),sqs.account-id(),sqs.name()))
                     .build())
                 .policyId(String.format("arn:aws:sqs:%s:%s:%s/SQSDefaultPolicy", sqs.region(),sqs.account-id(),sqs.name()))
                 .build());
     
             var snsTopic = new Topic("snsTopic", TopicArgs.builder()
                 .name(sns.name())
                 .displayName(sns.display_name())
                 .policy(snsTopicPolicy.json())
                 .build());
     
             var sqsQueue = new Queue("sqsQueue", QueueArgs.builder()
                 .name(sqs.name())
                 .policy(sqsQueuePolicy.json())
                 .build());
     
             var snsTopicTopicSubscription = new TopicSubscription("snsTopicTopicSubscription", TopicSubscriptionArgs.builder()
                 .topic(snsTopic.arn())
                 .protocol("sqs")
                 .endpoint(sqsQueue.arn())
                 .build());
     
         }
     }
     
     
    ### Example with Delivery Policy This example demonstrates how to define a `deliveryPolicy` for an HTTPS subscription. Unlike the `aws.sns.Topic` resource, the `deliveryPolicy` for `aws.sns.TopicSubscription` should not be wrapped in an `"http"` object.
     
     package generated_program;
     
     import com.pulumi.Context;
     import com.pulumi.Pulumi;
     import com.pulumi.core.Output;
     import com.pulumi.aws.sns.TopicSubscription;
     import com.pulumi.aws.sns.TopicSubscriptionArgs;
     import java.util.ArrayList;
     import java.util.Arrays;
     import java.util.Map;
     import java.io.File;
     import java.nio.file.Files;
     import java.nio.file.Paths;
     
     public class App {
         public static void main(String[] args) {
             Pulumi.run(App::stack);
         }
     
         public static void stack(Context ctx) {
             var exampleWithDeliveryPolicy = new TopicSubscription("exampleWithDeliveryPolicy", TopicSubscriptionArgs.builder()
                 .topic("arn:aws:sns:us-west-2:123456789012:my-topic")
                 .protocol("https")
                 .endpoint("https://example.com/endpoint")
                 .rawMessageDelivery(true)
                 .deliveryPolicy("""
     {
       \"healthyRetryPolicy\": {
         \"minDelayTarget\": 20,
         \"maxDelayTarget\": 20,
         \"numRetries\": 3,
         \"numMaxDelayRetries\": 0,
         \"numNoDelayRetries\": 0,
         \"numMinDelayRetries\": 0,
         \"backoffFunction\": \"linear\"
       },
       \"sicklyRetryPolicy\": null,
       \"throttlePolicy\": null,
       \"requestPolicy\": {
         \"headerContentType\": \"text/plain; application/json\"
       },
       \"guaranteed\": false
     }
                 """)
                 .build());
     
         }
     }
     
     
    ## Import ### Identity Schema #### Required - `arn` (String) ARN of the SNS topic subscription. Using `pulumi import`, import SNS Topic Subscriptions using the subscription `arn`. For example: ```sh $ pulumi import aws:sns/topicSubscription:TopicSubscription user_updates_sqs_target arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f ```
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.pulumi.resources.CustomResource

        com.pulumi.resources.CustomResource.CustomResourceInternal
      • Nested classes/interfaces inherited from class com.pulumi.resources.Resource

        com.pulumi.resources.Resource.LazyField<T extends java.lang.Object>, com.pulumi.resources.Resource.LazyFields, com.pulumi.resources.Resource.ResourceInternal
    • Field Summary

      • Fields inherited from class com.pulumi.resources.Resource

        childResources, remote
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      com.pulumi.core.Output<java.lang.String> arn()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Integer>> confirmationTimeoutInMinutes()  
      com.pulumi.core.Output<java.lang.Boolean> confirmationWasAuthenticated()  
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> deliveryPolicy()  
      com.pulumi.core.Output<java.lang.String> endpoint()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> endpointAutoConfirms()  
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> filterPolicy()  
      com.pulumi.core.Output<java.lang.String> filterPolicyScope()  
      static TopicSubscription get​(java.lang.String name, com.pulumi.core.Output<java.lang.String> id, TopicSubscriptionState state, com.pulumi.resources.CustomResourceOptions options)
      Get an existing Host resource's state with the given name, ID, and optional extra properties used to qualify the lookup.
      com.pulumi.core.Output<java.lang.String> ownerId()  
      com.pulumi.core.Output<java.lang.Boolean> pendingConfirmation()  
      com.pulumi.core.Output<java.lang.String> protocol()  
      com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> rawMessageDelivery()  
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> redrivePolicy()  
      com.pulumi.core.Output<java.lang.String> region()  
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> replayPolicy()  
      com.pulumi.core.Output<java.util.Optional<java.lang.String>> subscriptionRoleArn()  
      com.pulumi.core.Output<java.lang.String> topic()  
      • Methods inherited from class com.pulumi.resources.CustomResource

        getId, id, idFuture
      • Methods inherited from class com.pulumi.resources.Resource

        getChildResources, getResourceName, getResourceType, getUrn, pulumiChildResources, pulumiResourceName, pulumiResourceType, urn
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TopicSubscription

        public TopicSubscription​(java.lang.String name)
        Parameters:
        name - The _unique_ name of the resulting resource.
      • TopicSubscription

        public TopicSubscription​(java.lang.String name,
                                 TopicSubscriptionArgs args)
        Parameters:
        name - The _unique_ name of the resulting resource.
        args - The arguments to use to populate this resource's properties.
      • TopicSubscription

        public TopicSubscription​(java.lang.String name,
                                 TopicSubscriptionArgs args,
                                 @Nullable
                                 com.pulumi.resources.CustomResourceOptions options)
        Parameters:
        name - The _unique_ name of the resulting resource.
        args - The arguments to use to populate this resource's properties.
        options - A bag of options that control this resource's behavior.
    • Method Detail

      • arn

        public com.pulumi.core.Output<java.lang.String> arn()
        Returns:
        ARN of the subscription.
      • confirmationTimeoutInMinutes

        public com.pulumi.core.Output<java.util.Optional<java.lang.Integer>> confirmationTimeoutInMinutes()
        Returns:
        Integer indicating number of minutes to wait in retrying mode for fetching subscription ARN before marking it as failure. Only applicable for HTTP and HTTPS protocols. Default is `1`.
      • confirmationWasAuthenticated

        public com.pulumi.core.Output<java.lang.Boolean> confirmationWasAuthenticated()
        Returns:
        Whether the subscription confirmation request was authenticated.
      • deliveryPolicy

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> deliveryPolicy()
        Returns:
        JSON String with the delivery policy (retries, backoff, etc.) that will be used in the subscription - this only applies to HTTP/S subscriptions. Refer to the [SNS docs](https://docs.aws.amazon.com/sns/latest/dg/DeliveryPolicies.html) for more details.
      • endpoint

        public com.pulumi.core.Output<java.lang.String> endpoint()
        Returns:
        Endpoint to send data to. The contents vary with the protocol. See details below.
      • endpointAutoConfirms

        public com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> endpointAutoConfirms()
        Returns:
        Whether the endpoint is capable of [auto confirming subscription](http://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.html#SendMessageToHttp.prepare) (e.g., PagerDuty). Default is `false`.
      • filterPolicy

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> filterPolicy()
        Returns:
        JSON String with the filter policy that will be used in the subscription to filter messages seen by the target resource. Refer to the [SNS docs](https://docs.aws.amazon.com/sns/latest/dg/message-filtering.html) for more details.
      • filterPolicyScope

        public com.pulumi.core.Output<java.lang.String> filterPolicyScope()
        Returns:
        Whether the `filterPolicy` applies to `MessageAttributes` (default) or `MessageBody`.
      • ownerId

        public com.pulumi.core.Output<java.lang.String> ownerId()
        Returns:
        AWS account ID of the subscription's owner.
      • pendingConfirmation

        public com.pulumi.core.Output<java.lang.Boolean> pendingConfirmation()
        Returns:
        Whether the subscription has not been confirmed.
      • protocol

        public com.pulumi.core.Output<java.lang.String> protocol()
        Returns:
        Protocol to use. Valid values are: `sqs`, `sms`, `lambda`, `firehose`, and `application`. Protocols `email`, `email-json`, `http` and `https` are also valid but partially supported. See details below.
      • rawMessageDelivery

        public com.pulumi.core.Output<java.util.Optional<java.lang.Boolean>> rawMessageDelivery()
        Returns:
        Whether to enable raw message delivery (the original message is directly passed, not wrapped in JSON with the original message in the message property). Default is `false`.
      • redrivePolicy

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> redrivePolicy()
        Returns:
        JSON String with the redrive policy that will be used in the subscription. Refer to the [SNS docs](https://docs.aws.amazon.com/sns/latest/dg/sns-dead-letter-queues.html#how-messages-moved-into-dead-letter-queue) for more details.
      • region

        public com.pulumi.core.Output<java.lang.String> region()
        Returns:
        Region where this resource will be [managed](https://docs.aws.amazon.com/general/latest/gr/rande.html#regional-endpoints). Defaults to the Region set in the provider configuration.
      • replayPolicy

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> replayPolicy()
        Returns:
        JSON String with the archived message replay policy that will be used in the subscription. Refer to the [SNS docs](https://docs.aws.amazon.com/sns/latest/dg/message-archiving-and-replay-subscriber.html) for more details.
      • subscriptionRoleArn

        public com.pulumi.core.Output<java.util.Optional<java.lang.String>> subscriptionRoleArn()
        Returns:
        ARN of the IAM role to publish to Kinesis Data Firehose delivery stream. Refer to [SNS docs](https://docs.aws.amazon.com/sns/latest/dg/sns-firehose-as-subscriber.html).
      • topic

        public com.pulumi.core.Output<java.lang.String> topic()
        Returns:
        ARN of the SNS topic to subscribe to. The following arguments are optional:
      • get

        public static TopicSubscription get​(java.lang.String name,
                                            com.pulumi.core.Output<java.lang.String> id,
                                            @Nullable
                                            TopicSubscriptionState state,
                                            @Nullable
                                            com.pulumi.resources.CustomResourceOptions options)
        Get an existing Host resource's state with the given name, ID, and optional extra properties used to qualify the lookup.
        Parameters:
        name - The _unique_ name of the resulting resource.
        id - The _unique_ provider ID of the resource to lookup.
        state -
        options - Optional settings to control the behavior of the CustomResource.