RegistryPolicy class

Provides an Elastic Container Registry Policy.

> NOTE on ECR Registry Policies: While the AWS Management Console interface may suggest the ability to define multiple policies by creating multiple statements, ECR registry policies are effectively managed as singular entities at the regional level by the AWS APIs. Therefore, the aws.ecr.RegistryPolicy resource should be configured only once per region with all necessary statements defined in the same policy. Attempting to define multiple aws.ecr.RegistryPolicy resources may result in perpetual differences, with one policy overriding another.

Example Usage

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

const current = aws.getCallerIdentity({});
const currentGetRegion = aws.getRegion({});
const currentGetPartition = aws.getPartition({});
const example = new aws.ecr.RegistryPolicy("example", {policy: JSON.stringify({
    Version: "2012-10-17",
    Statement: [{
        Sid: "testpolicy",
        Effect: "Allow",
        Principal: {
            AWS: Promise.all([currentGetPartition, current]).then(([currentGetPartition, current]) => `arn:${currentGetPartition.partition}:iam::${current.accountId}:root`),
        },
        Action: ["ecr:ReplicateImage"],
        Resource: [Promise.all([currentGetPartition, currentGetRegion, current]).then(([currentGetPartition, currentGetRegion, current]) => `arn:${currentGetPartition.partition}:ecr:${currentGetRegion.region}:${current.accountId}:repository/*`)],
    }],
})});
import pulumi
import json
import pulumi_aws as aws

current = aws.get_caller_identity()
current_get_region = aws.get_region()
current_get_partition = aws.get_partition()
example = aws.ecr.RegistryPolicy("example", policy=json.dumps({
    "Version": "2012-10-17",
    "Statement": [{
        "Sid": "testpolicy",
        "Effect": "Allow",
        "Principal": {
            "AWS": f"arn:{current_get_partition.partition}:iam::{current.account_id}:root",
        },
        "Action": ["ecr:ReplicateImage"],
        "Resource": [f"arn:{current_get_partition.partition}:ecr:{current_get_region.region}:{current.account_id}:repository/*"],
    }],
}))
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Pulumi;
using Aws = Pulumi.Aws;

return await Deployment.RunAsync(() =>
{
    var current = Aws.GetCallerIdentity.Invoke();

    var currentGetRegion = Aws.GetRegion.Invoke();

    var currentGetPartition = Aws.GetPartition.Invoke();

    var example = new Aws.Ecr.RegistryPolicy("example", new()
    {
        Policy = JsonSerializer.Serialize(new Dictionary<string, object?>
        {
            ["Version"] = "2012-10-17",
            ["Statement"] = new[]
            {
                new Dictionary<string, object?>
                {
                    ["Sid"] = "testpolicy",
                    ["Effect"] = "Allow",
                    ["Principal"] = new Dictionary<string, object?>
                    {
                        ["AWS"] = Output.Tuple(currentGetPartition, current).Apply(values =>
                        {
                            var currentGetPartition = values.Item1;
                            var current = values.Item2;
                            return $"arn:{currentGetPartition.Apply(getPartitionResult => getPartitionResult.Partition)}:iam::{current.Apply(getCallerIdentityResult => getCallerIdentityResult.AccountId)}:root";
                        }),
                    },
                    ["Action"] = new[]
                    {
                        "ecr:ReplicateImage",
                    },
                    ["Resource"] = new[]
                    {
                        Output.Tuple(currentGetPartition, currentGetRegion, current).Apply(values =>
                        {
                            var currentGetPartition = values.Item1;
                            var currentGetRegion = values.Item2;
                            var current = values.Item3;
                            return $"arn:{currentGetPartition.Apply(getPartitionResult => getPartitionResult.Partition)}:ecr:{currentGetRegion.Apply(getRegionResult => getRegionResult.Region)}:{current.Apply(getCallerIdentityResult => getCallerIdentityResult.AccountId)}:repository/*";
                        }),
                    },
                },
            },
        }),
    });

});
package main

import (
	"encoding/json"
	"fmt"

	"github.com/pulumi/pulumi-aws/sdk/v7/go/aws"
	"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/ecr"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		current, err := aws.GetCallerIdentity(ctx, &aws.GetCallerIdentityArgs{}, nil)
		if err != nil {
			return err
		}
		currentGetRegion, err := aws.GetRegion(ctx, &aws.GetRegionArgs{}, nil)
		if err != nil {
			return err
		}
		currentGetPartition, err := aws.GetPartition(ctx, &aws.GetPartitionArgs{}, nil)
		if err != nil {
			return err
		}
		tmpJSON0, err := json.Marshal(map[string]interface{}{
			"Version": "2012-10-17",
			"Statement": []map[string]interface{}{
				map[string]interface{}{
					"Sid":    "testpolicy",
					"Effect": "Allow",
					"Principal": map[string]string{
						"AWS": fmt.Sprintf("arn:%v:iam::%v:root", currentGetPartition.Partition, current.AccountId),
					},
					"Action": []string{
						"ecr:ReplicateImage",
					},
					"Resource": []string{
						fmt.Sprintf("arn:%v:ecr:%v:%v:repository/*", currentGetPartition.Partition, currentGetRegion.Region, current.AccountId),
					},
				},
			},
		})
		if err != nil {
			return err
		}
		json0 := string(tmpJSON0)
		_, err = ecr.NewRegistryPolicy(ctx, "example", &ecr.RegistryPolicyArgs{
			Policy: json0,
		})
		if err != nil {
			return err
		}
		return nil
	})
}
pulumi {
  required_providers {
    aws = {
      source = "pulumi/aws"
    }
  }
}

data "aws_getcalleridentity" "current" {
}
data "aws_getregion" "currentGetRegion" {
}
data "aws_getpartition" "currentGetPartition" {
}

resource "aws_ecr_registrypolicy" "example" {
  policy = jsonencode({
    "Version" = "2012-10-17"
    "Statement" = [{
      "Sid"    = "testpolicy"
      "Effect" = "Allow"
      "Principal" = {
        "AWS" ="arn:${data.aws_getpartition.currentGetPartition.partition}:iam::${data.aws_getcalleridentity.current.account_id}:root"
      }
      "Action"   = ["ecr:ReplicateImage"]
      "Resource" = ["arn:${data.aws_getpartition.currentGetPartition.partition}:ecr:${data.aws_getregion.currentGetRegion.region}:${data.aws_getcalleridentity.current.account_id}:repository/*"]
    }]
  })
}
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.AwsFunctions;
import com.pulumi.aws.inputs.GetCallerIdentityArgs;
import com.pulumi.aws.inputs.GetRegionArgs;
import com.pulumi.aws.inputs.GetPartitionArgs;
import com.pulumi.aws.ecr.RegistryPolicy;
import com.pulumi.aws.ecr.RegistryPolicyArgs;
import static com.pulumi.codegen.internal.Serialization.*;
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 current = AwsFunctions.getCallerIdentity(GetCallerIdentityArgs.builder()
            .build());

        final var currentGetRegion = AwsFunctions.getRegion(GetRegionArgs.builder()
            .build());

        final var currentGetPartition = AwsFunctions.getPartition(GetPartitionArgs.builder()
            .build());

        var example = new RegistryPolicy("example", RegistryPolicyArgs.builder()
            .policy(serializeJson(
                jsonObject(
                    jsonProperty("Version", "2012-10-17"),
                    jsonProperty("Statement", jsonArray(jsonObject(
                        jsonProperty("Sid", "testpolicy"),
                        jsonProperty("Effect", "Allow"),
                        jsonProperty("Principal", jsonObject(
                            jsonProperty("AWS", String.format("arn:%s:iam::%s:root", currentGetPartition.partition(),current.accountId()))
                        )),
                        jsonProperty("Action", jsonArray("ecr:ReplicateImage")),
                        jsonProperty("Resource", jsonArray(String.format("arn:%s:ecr:%s:%s:repository/*", currentGetPartition.partition(),currentGetRegion.region(),current.accountId())))
                    )))
                )))
            .build());

    }
}
resources:
  example:
    type: aws:ecr:RegistryPolicy
    properties:
      policy:
        fn::toJSON:
          Version: 2012-10-17
          Statement:
            - Sid: testpolicy
              Effect: Allow
              Principal:
                AWS: arn:${currentGetPartition.partition}:iam::${current.accountId}:root
              Action:
                - ecr:ReplicateImage
              Resource:
                - arn:${currentGetPartition.partition}:ecr:${currentGetRegion.region}:${current.accountId}:repository/*
variables:
  current:
    fn::invoke:
      function: aws:getCallerIdentity
      arguments: {}
  currentGetRegion:
    fn::invoke:
      function: aws:getRegion
      arguments: {}
  currentGetPartition:
    fn::invoke:
      function: aws:getPartition
      arguments: {}

Import

Using pulumi import, import ECR Registry Policy using the registry id. For example:

$ pulumi import aws:ecr/registryPolicy:RegistryPolicy example 123456789012

Constructors

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

Properties

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
policy ↔ Output<String>
The policy document. This is a JSON formatted string.
latefinal
region ↔ Output<String>
Region where this resource will be managed. Defaults to the Region set in the provider configuration.
latefinal
registryId ↔ Output<String>
The registry ID where the registry was created.
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, {RegistryPolicyState? state, CustomResourceOptions? options}) → RegistryPolicy
Gets an existing RegistryPolicy resource's state with the given name and id.