---
# System prepended metadata

title: Query 317

---

To set a repository policy for an image repository in Amazon Elastic Container Registry (ECR) using C++, you can use the AWS SDK for C++. The repository policy allows you to define access controls and permissions for the ECR repository. Here's a code snippet to set a repository policy:
```
#include <aws/core/Aws.h>
#include <aws/ecr/ECRClient.h>
#include <aws/ecr/model/SetRepositoryPolicyRequest.h>
#include <iostream>

int main() {
    Aws::SDKOptions options;
    Aws::InitAPI(options);

    // Initialize your AWS credentials and region
    Aws::String region = "your-region";
    Aws::Auth::AWSCredentials credentials("your-access-key", "your-secret-key");

    Aws::Client::ClientConfiguration clientConfig;
    clientConfig.region = region;

    Aws::ECR::ECRClient ecrClient(credentials, clientConfig);

    // Define the ECR repository name
    Aws::String repositoryName = "your-ecr-repo-name";

    // Define the repository policy document in JSON format
    Aws::String repositoryPolicy = R"(
        {
            "Version": "2008-10-17",
            "Statement": [
                {
                    "Sid": "AllowPull",
                    "Effect": "Allow",
                    "Principal": "*",
                    "Action": [
                        "ecr:GetDownloadUrlForLayer",
                        "ecr:GetRepositoryPolicy",
                        "ecr:BatchCheckLayerAvailability",
                        "ecr:GetAuthorizationToken",
                        "ecr:GetMetadata",
                        "ecr:DescribeImages",
                        "ecr:GetObject",
                        "ecr:BatchGetImage",
                        "ecr:GetManifest"
                    ]
                }
            ]
        }
    )";

    // Set the repository policy
    Aws::ECR::Model::SetRepositoryPolicyRequest policyRequest;
    policyRequest.SetRepositoryName(repositoryName);
    policyRequest.SetPolicyText(repositoryPolicy);

    Aws::ECR::Model::SetRepositoryPolicyOutcome policyOutcome = ecrClient.SetRepositoryPolicy(policyRequest);
    if (policyOutcome.IsSuccess()) {
        std::cout << "Repository policy set successfully." << std::endl;
    } else {
        std::cerr << "Failed to set repository policy: " << policyOutcome.GetError().GetMessage() << std::endl;
    }

    Aws::ShutdownAPI(options);
    return 0;
}
```
In this code:
* Replace `your-region, your-access-key, your-secret-key, and your-ecr-repo-name` with your specific AWS configuration and ECR repository details.
* The repositoryPolicy variable contains the JSON-formatted repository policy document, which you can customize to define your desired permissions.
* The code sets the repository policy for the specified ECR repository, granting permissions based on the defined policy.

This C++ code sets a repository policy for an Amazon Elastic Container Registry (ECR) repository using the AWS SDK for C++. It begins by initializing the AWS SDK, specifying the AWS region, and providing AWS credentials. It defines the ECR repository name and a JSON-formatted repository policy, which allows specific actions for all principals (``*``). The code then creates a request to set the repository policy, including the repository name and the policy document. After executing this request, it checks the outcome, displaying a success message if the policy is set or an error message if the operation fails. This code ensures controlled access to the ECR repository based on the defined policy.