TopicPolicy class

Provides an SNS topic policy resource

> NOTE: If a Principal is specified as just an AWS account ID rather than an ARN, AWS silently converts it to the ARN for the root user, causing future deployments to differ. To avoid this problem, just specify the full ARN, e.g. arn:aws:iam::123456789012:root

Example Usage

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const test = new aws.sns.Topic("test", {name: "my-topic-with-policy"});
const snsTopicPolicy = aws.iam.getPolicyDocumentOutput({
    statements: [{
        conditions: [{
            test: "StringEquals",
            variable: "AWS:SourceOwner",
            values: [account_id],
        }],
        principals: [{
            type: "AWS",
            identifiers: ["*"],
        }],
        actions: [
            "SNS:Subscribe",
            "SNS:SetTopicAttributes",
            "SNS:RemovePermission",
            "SNS:Receive",
            "SNS:Publish",
            "SNS:ListSubscriptionsByTopic",
            "SNS:GetTopicAttributes",
            "SNS:DeleteTopic",
            "SNS:AddPermission",
        ],
        effect: "Allow",
        resources: [test.arn],
        sid: "__default_statement_ID",
    }],
    policyId: "__default_policy_ID",
});
const _default = new aws.sns.TopicPolicy("default", {
    arn: test.arn,
    policy: snsTopicPolicy.json,
});
import pulumi
import pulumi_aws as aws

test = aws.sns.Topic("test", name="my-topic-with-policy")
sns_topic_policy = aws.iam.get_policy_document_output(statements=[{
        "conditions": [{
            "test": "StringEquals",
            "variable": "AWS:SourceOwner",
            "values": [account_id],
        }],
        "principals": [{
            "type": "AWS",
            "identifiers": ["*"],
        }],
        "actions": [
            "SNS:Subscribe",
            "SNS:SetTopicAttributes",
            "SNS:RemovePermission",
            "SNS:Receive",
            "SNS:Publish",
            "SNS:ListSubscriptionsByTopic",
            "SNS:GetTopicAttributes",
            "SNS:DeleteTopic",
            "SNS:AddPermission",
        ],
        "effect": "Allow",
        "resources": [test.arn],
        "sid": "__default_statement_ID",
    }],
    policy_id="__default_policy_ID")
default = aws.sns.TopicPolicy("default",
    arn=test.arn,
    policy=sns_topic_policy.json)
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Aws = Pulumi.Aws;

return await Deployment.RunAsync(() =>
{
    var test = new Aws.Sns.Topic("test", new()
    {
        Name = "my-topic-with-policy",
    });

    var snsTopicPolicy = Aws.Iam.GetPolicyDocument.Invoke(new()
    {
        Statements = new[]
        {
            new Aws.Iam.Inputs.GetPolicyDocumentStatementInputArgs
            {
                Conditions = new[]
                {
                    new Aws.Iam.Inputs.GetPolicyDocumentStatementConditionInputArgs
                    {
                        Test = "StringEquals",
                        Variable = "AWS:SourceOwner",
                        Values = new[]
                        {
                            account_id,
                        },
                    },
                },
                Principals = new[]
                {
                    new Aws.Iam.Inputs.GetPolicyDocumentStatementPrincipalInputArgs
                    {
                        Type = "AWS",
                        Identifiers = new[]
                        {
                            "*",
                        },
                    },
                },
                Actions = new[]
                {
                    "SNS:Subscribe",
                    "SNS:SetTopicAttributes",
                    "SNS:RemovePermission",
                    "SNS:Receive",
                    "SNS:Publish",
                    "SNS:ListSubscriptionsByTopic",
                    "SNS:GetTopicAttributes",
                    "SNS:DeleteTopic",
                    "SNS:AddPermission",
                },
                Effect = "Allow",
                Resources = new[]
                {
                    test.Arn,
                },
                Sid = "__default_statement_ID",
            },
        },
        PolicyId = "__default_policy_ID",
    });

    var @default = new Aws.Sns.TopicPolicy("default", new()
    {
        Arn = test.Arn,
        Policy = snsTopicPolicy.Apply(getPolicyDocumentResult => getPolicyDocumentResult.Json),
    });

});
package main

import (
	"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/iam"
	"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/sns"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		test, err := sns.NewTopic(ctx, "test", &sns.TopicArgs{
			Name: pulumi.String("my-topic-with-policy"),
		})
		if err != nil {
			return err
		}
		snsTopicPolicy := iam.GetPolicyDocumentOutput(ctx, iam.GetPolicyDocumentOutputArgs{
			Statements: iam.GetPolicyDocumentStatementArray{
				&iam.GetPolicyDocumentStatementArgs{
					Conditions: iam.GetPolicyDocumentStatementConditionArray{
						&iam.GetPolicyDocumentStatementConditionArgs{
							Test:     pulumi.String("StringEquals"),
							Variable: pulumi.String("AWS:SourceOwner"),
							Values: pulumi.StringArray{
								account_id,
							},
						},
					},
					Principals: iam.GetPolicyDocumentStatementPrincipalArray{
						&iam.GetPolicyDocumentStatementPrincipalArgs{
							Type: pulumi.String("AWS"),
							Identifiers: pulumi.StringArray{
								pulumi.String("*"),
							},
						},
					},
					Actions: pulumi.StringArray{
						pulumi.String("SNS:Subscribe"),
						pulumi.String("SNS:SetTopicAttributes"),
						pulumi.String("SNS:RemovePermission"),
						pulumi.String("SNS:Receive"),
						pulumi.String("SNS:Publish"),
						pulumi.String("SNS:ListSubscriptionsByTopic"),
						pulumi.String("SNS:GetTopicAttributes"),
						pulumi.String("SNS:DeleteTopic"),
						pulumi.String("SNS:AddPermission"),
					},
					Effect: pulumi.String("Allow"),
					Resources: pulumi.StringArray{
						test.Arn,
					},
					Sid: pulumi.String("__default_statement_ID"),
				},
			},
			PolicyId: pulumi.String("__default_policy_ID"),
		}, nil)
		_, err = sns.NewTopicPolicy(ctx, "default", &sns.TopicPolicyArgs{
			Arn:    test.Arn,
			Policy: snsTopicPolicy.Json(),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
pulumi {
  required_providers {
    aws = {
      source = "pulumi/aws"
    }
  }
}

data "aws_iam_getpolicydocument" "snsTopicPolicy" {
  statements {
    conditions {
      test     = "StringEquals"
      variable = "AWS:SourceOwner"
      values   = [account-id]
    }
    principals {
      type        = "AWS"
      identifiers = ["*"]
    }
    actions   = ["SNS:Subscribe", "SNS:SetTopicAttributes", "SNS:RemovePermission", "SNS:Receive", "SNS:Publish", "SNS:ListSubscriptionsByTopic", "SNS:GetTopicAttributes", "SNS:DeleteTopic", "SNS:AddPermission"]
    effect    = "Allow"
    resources = [aws_sns_topic.test.arn]
    sid       = "__default_statement_ID"
  }
  policy_id = "__default_policy_ID"
}

resource "aws_sns_topic" "test" {
  name = "my-topic-with-policy"
}
resource "aws_sns_topicpolicy" "default" {
  arn    = aws_sns_topic.test.arn
  policy = data.aws_iam_getpolicydocument.snsTopicPolicy.json
}
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.sns.TopicPolicy;
import com.pulumi.aws.sns.TopicPolicyArgs;
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 test = new Topic("test", TopicArgs.builder()
            .name("my-topic-with-policy")
            .build());

        final var snsTopicPolicy = IamFunctions.getPolicyDocument(GetPolicyDocumentArgs.builder()
            .statements(GetPolicyDocumentStatementArgs.builder()
                .conditions(GetPolicyDocumentStatementConditionArgs.builder()
                    .test("StringEquals")
                    .variable("AWS:SourceOwner")
                    .values(account_id)
                    .build())
                .principals(GetPolicyDocumentStatementPrincipalArgs.builder()
                    .type("AWS")
                    .identifiers("*")
                    .build())
                .actions(
                    "SNS:Subscribe",
                    "SNS:SetTopicAttributes",
                    "SNS:RemovePermission",
                    "SNS:Receive",
                    "SNS:Publish",
                    "SNS:ListSubscriptionsByTopic",
                    "SNS:GetTopicAttributes",
                    "SNS:DeleteTopic",
                    "SNS:AddPermission")
                .effect("Allow")
                .resources(test.arn())
                .sid("__default_statement_ID")
                .build())
            .policyId("__default_policy_ID")
            .build());

        var default_ = new TopicPolicy("default", TopicPolicyArgs.builder()
            .arn(test.arn())
            .policy(snsTopicPolicy.applyValue(_snsTopicPolicy -> _snsTopicPolicy.json()))
            .build());

    }
}
resources:
  test:
    type: aws:sns:Topic
    properties:
      name: my-topic-with-policy
  default:
    type: aws:sns:TopicPolicy
    properties:
      arn: ${test.arn}
      policy: ${snsTopicPolicy.json}
variables:
  snsTopicPolicy:
    fn::invoke:
      function: aws:iam:getPolicyDocument
      arguments:
        statements:
          - conditions:
              - test: StringEquals
                variable: AWS:SourceOwner
                values:
                  - ${["account-id"]}
            principals:
              - type: AWS
                identifiers:
                  - '*'
            actions:
              - SNS:Subscribe
              - SNS:SetTopicAttributes
              - SNS:RemovePermission
              - SNS:Receive
              - SNS:Publish
              - SNS:ListSubscriptionsByTopic
              - SNS:GetTopicAttributes
              - SNS:DeleteTopic
              - SNS:AddPermission
            effect: Allow
            resources:
              - ${test.arn}
            sid: __default_statement_ID
        policyId: __default_policy_ID

Import

Identity Schema

Required

  • arn (String) ARN of the SNS topic.

Using pulumi import, import SNS Topic Policy using the topic ARN. For example:

$ pulumi import aws:sns/topicPolicy:TopicPolicy user_updates arn:aws:sns:us-west-2:123456789012:my-topic

Constructors

TopicPolicy(String name, {TopicPolicyArgs? args, CustomResourceOptions? options})
Creates a new TopicPolicy. name The Pulumi resource name. args Arguments used to configure this TopicPolicy. The set of arguments for TopicPolicy. options Resource options controlling this resource's behavior.
TopicPolicy.reference(String urn)
Creates a typed reference to an existing TopicPolicy resource.

Properties

arn ↔ Output<String>
The ARN of the SNS topic
latefinal
childResources → Set<Resource>
finalinherited
completionSources ↔ Map<String, IOutputCompletionSource>
latefinalinherited
hashCode → int
The hash code for this object.
no setterinherited
id ↔ Output<String>
getter/setter pairinherited
isCustom → bool
Returns whether this resource is provider-managed.
no setterinherited
isProtected → bool
Returns whether this resource is protected from deletion.
no setterinherited
isRemote → bool
Whether this resource is registered as remote.
no setterinherited
isResourceReference → bool
Whether this instance represents a resource value returned over RPC.
finalinherited
owner ↔ Output<String>
The AWS Account ID of the SNS topic owner
latefinal
policy ↔ Output<String>
The fully-formed AWS policy as JSON.
latefinal
region ↔ Output<String>
Region where this resource will be managed. Defaults to the Region set in the provider configuration.
latefinal
resourceTransforms → List<ResourceTransform>
Inherited/explicit async transforms.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
transformations → List<ResourceTransformation>
Inherited/explicit legacy transformations.
no setterinherited
urn ↔ Output<String>
latefinalinherited

Methods

failId(Object error) → void
Completes this resource ID with an error when registration fails.
inherited
failOutputs(Object error) → void
Completes all output properties with error.
inherited
failUrn(Object error) → void
Completes this resource URN with an error when registration fails.
inherited
getProvider(String moduleMember) → ProviderResource?
Returns provider for moduleMember's package, if configured.
inherited
getResourceName() → String
Returns this resource's logical name.
inherited
getResourceType() → String
Returns this resource's Pulumi type token.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerOutput<T>(String propertyName, {Object? decoder(Object?)?, bool isSecret = false}) → Output<T>
Registers a dynamic output property for this resource.
inherited
resolveId(String? value, {required bool isKnown}) → void
Resolves the provider-assigned ID for this resource.
inherited
resolveOutputs(Struct outputs) → void
Resolves all output properties from a monitor response payload.
inherited
resolveUrn(String value) → void
Resolves this resource's URN once assigned by the engine.
inherited
serializeProperties(Map<String, dynamic> properties) → Future<Struct>
Serializes resource properties for RPC transmission.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

get(String name, Input<String> id, {TopicPolicyState? state, CustomResourceOptions? options}) → TopicPolicy
Gets an existing TopicPolicy resource's state with the given name and id.