

# Set up a CA for AWS IAM for agents and workloads (AWS CLI)

This topic shows how to set up a certificate authority (CA) for AWS IAM for agents and workloads with the AWS CLI. You create and activate a service-affiliated CA in the US East (N. Virginia) Region (`us-east-1`), and replicate it to the Regions where your workloads run. If the workload identity provider is in another account, you grant that account access to the CA. You then attach the CA to the provider in IAM, and your workloads can request certificates from the CA.

## Prerequisites
<a name="workload-identity-prerequisites"></a>

Before you begin, make sure that you have the following:
+ [AWS CLI v2](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) installed and configured.
+ A workload identity provider in IAM. To create one, see [Agent and workload identity](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) in the *IAM User Guide*.
+ If you want a subordinate CA, a parent CA that signs its certificate signing request (CSR). Otherwise, the example in this topic creates a root CA.
+ Your AWS account enabled in every Region that you replicate the CA to.
+ The IAM permissions in the following policy.

With the following identity-based policy, you can create, activate, replicate, and delete a CA for AWS IAM for agents and workloads. The `Resource` uses a wildcard for the Region because the primary CA and its replicas share one CA ID.

The replica operations create each replica in the target Region with your credentials. The policy must therefore allow `acm-pca:CreateCertificateAuthority` in every Region that you replicate to. When you attach the CA to a workload identity provider, IAM calls AWS Private CA with your credentials. The policy covers the AWS Private CA permissions for attaching and detaching the CA. For the IAM permissions that attaching the CA needs, see [Agent and workload identity](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) in the *IAM User Guide*. Add `acm-pca:TagCertificateAuthority` if you tag the CA or its replicas, and `acm-pca:CreatePermission` if the provider is in another account.

```json
{
  "Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "ManageWorkloadIdentityCA",
      "Effect": "Allow",
      "Action": [
        "acm-pca:CreateCertificateAuthority",
        "acm-pca:DescribeCertificateAuthority",
        "acm-pca:GetCertificateAuthorityCsr",
        "acm-pca:GetCertificateAuthorityCertificate",
        "acm-pca:IssueCertificate",
        "acm-pca:GetCertificate",
        "acm-pca:ImportCertificateAuthorityCertificate",
        "acm-pca:CreateCertificateAuthorityReplica",
        "acm-pca:ListCertificateAuthorityReplicas",
        "acm-pca:CreateCertificateAuthorityAssociation",
        "acm-pca:DeleteCertificateAuthorityAssociation",
        "acm-pca:UpdateCertificateAuthority",
        "acm-pca:DeleteCertificateAuthority"
      ],
      "Resource": "arn:aws:acm-pca:*:{{123456789012}}:certificate-authority/*"
    },
    {
      "Sid": "ListCertificateAuthorities",
      "Effect": "Allow",
      "Action": "acm-pca:ListCertificateAuthorities",
      "Resource": "*"
    }
  ]
}
```

Replace {{123456789012}} with the ID of the AWS account that owns the CA.

## Step 1: Create the CA
<a name="workload-identity-create-ca"></a>

You must create a CA for AWS IAM for agents and workloads in the US East (N. Virginia) Region (`us-east-1`). That Region is the primary Region of the CA. You replicate the CA to other Regions in [Step 3: Replicate the CA to the Regions where workloads run](#workload-identity-replicate-ca).

First, create a file named `ca_config.json` that holds the CA configuration. A CA for AWS IAM for agents and workloads accepts the key algorithms `RSA_2048`, `RSA_3072`, `RSA_4096`, `EC_prime256v1`, `EC_secp384r1`, and `EC_secp521r1`.

```json
{
  "KeyAlgorithm": "EC_prime256v1",
  "SigningAlgorithm": "SHA256WITHECDSA",
  "Subject": {
    "CommonName": "{{workload-identity-ca.example.com}}"
  }
}
```

Then run the [create-certificate-authority](https://docs.aws.amazon.com/cli/latest/reference/acm-pca/create-certificate-authority.html) command. The `--usage-mode` and `--service-affiliation` parameters affiliate the CA with AWS IAM for agents and workloads.

```
$ aws acm-pca create-certificate-authority \
    --region us-east-1 \
    --certificate-authority-type ROOT \
    --certificate-authority-configuration file://ca_config.json \
    --usage-mode SERVICE_AFFILIATED \
    --service-affiliation ServiceAffiliationCode=IAM_WORKLOAD_IDENTITY
```

**Note**  
To create a CA that your own root CA signs, use `SUBORDINATE` instead of `ROOT` for `--certificate-authority-type`. Don't configure revocation. The affiliation turns off certificate revocation lists (CRLs) and Online Certificate Status Protocol (OCSP) for the CA.

The command returns the Amazon Resource Name (ARN) of the new CA.

```json
{
    "CertificateAuthorityArn": "arn:aws:acm-pca:us-east-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}}"
}
```

The new CA has the status `PENDING_CERTIFICATE` until you install its certificate.

## Step 2: Activate the CA
<a name="workload-identity-activate-ca"></a>

To activate the CA, retrieve its CSR, sign the CSR, and import the signed certificate. Use the [get-certificate-authority-csr](https://docs.aws.amazon.com/cli/latest/reference/acm-pca/get-certificate-authority-csr.html) command to retrieve the CSR.

```
$ aws acm-pca get-certificate-authority-csr \
    --region us-east-1 \
    --certificate-authority-arn arn:aws:acm-pca:us-east-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}} \
    --output text > ca.csr
```

For a root CA, the CA signs its own CSR. Choose a validity period for the CA certificate that covers the planned life of the CA. Use the [issue-certificate](https://docs.aws.amazon.com/cli/latest/reference/acm-pca/issue-certificate.html) command with the root CA certificate template.

```
$ aws acm-pca issue-certificate \
    --region us-east-1 \
    --certificate-authority-arn arn:aws:acm-pca:us-east-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}} \
    --csr fileb://ca.csr \
    --signing-algorithm SHA256WITHECDSA \
    --template-arn arn:aws:acm-pca:::template/RootCACertificate/V1 \
    --validity Value=10,Type=YEARS
```

The command returns the ARN of the CA certificate.

```json
{
    "CertificateArn": "arn:aws:acm-pca:us-east-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}}/certificate/{{6707447683a9b7f4055627ffd55cd4dc}}"
}
```

Retrieve the certificate with the [get-certificate](https://docs.aws.amazon.com/cli/latest/reference/acm-pca/get-certificate.html) command and save it to a file.

```
$ aws acm-pca get-certificate \
    --region us-east-1 \
    --certificate-authority-arn arn:aws:acm-pca:us-east-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}} \
    --certificate-arn arn:aws:acm-pca:us-east-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}}/certificate/{{6707447683a9b7f4055627ffd55cd4dc}} \
    --output text > ca.pem
```

Install the certificate with the [import-certificate-authority-certificate](https://docs.aws.amazon.com/cli/latest/reference/acm-pca/import-certificate-authority-certificate.html) command.

```
$ aws acm-pca import-certificate-authority-certificate \
    --region us-east-1 \
    --certificate-authority-arn arn:aws:acm-pca:us-east-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}} \
    --certificate fileb://ca.pem
```

For a subordinate CA, have your parent CA sign `ca.csr` instead. Then import the signed certificate together with its chain. The chain must be complete and end with the self-signed root certificate. You can't import a chain without the root certificate.

```
$ aws acm-pca import-certificate-authority-certificate \
    --region us-east-1 \
    --certificate-authority-arn arn:aws:acm-pca:us-east-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}} \
    --certificate fileb://ca.crt \
    --certificate-chain fileb://ca-chain.pem
```

For the import restrictions on a CA with this affiliation, see [Operations on a CA for AWS IAM for agents and workloads](PcaWorkloadIdentity-considerations.md#workload-identity-considerations-operations).

After the import, **describe-certificate-authority** shows `"Status": "ACTIVE"`. You can replicate only an `ACTIVE` primary CA.

## Step 3: Replicate the CA to the Regions where workloads run
<a name="workload-identity-replicate-ca"></a>

Replicate the CA to every Region where your workloads run. AWS Security Token Service (AWS STS) issues workload certificates from the CA in the workload's own Region, so a Region without an `ACTIVE` replica can't issue certificates. You call the replica operations in the primary Region with the ARN of the primary CA. Each target Region must differ from the primary Region and must be enabled for your account.

To create replicas in up to 20 Regions in one call, use the [batch-create-certificate-authority-replica](https://docs.aws.amazon.com/cli/latest/reference/acm-pca/batch-create-certificate-authority-replica.html) command. Each entry names a Region and carries an idempotency token.

```
$ aws acm-pca batch-create-certificate-authority-replica \
    --region us-east-1 \
    --certificate-authority-arn arn:aws:acm-pca:us-east-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}} \
    --replicas 'ReplicaRegion=us-west-2,IdempotencyToken=usw2-001' 'ReplicaRegion=eu-west-1,IdempotencyToken=euw1-001'
```

The output lists the replicas that AWS Private CA started to create under `Successful`, and the Regions that it couldn't replicate to under `Failed`.

```json
{
    "Successful": [
        {
            "Arn": "arn:aws:acm-pca:us-west-2:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}}",
            "Region": "us-west-2",
            "Status": "CREATING"
        },
        {
            "Arn": "arn:aws:acm-pca:eu-west-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}}",
            "Region": "eu-west-1",
            "Status": "CREATING"
        }
    ],
    "Failed": []
}
```

If a Region appears under `Failed`, fix the cause and retry that Region. For the causes, see [Troubleshoot CAs for AWS IAM for agents and workloads](PcaWorkloadIdentity-troubleshoot.md).

To replicate to a single Region, use the [create-certificate-authority-replica](https://docs.aws.amazon.com/cli/latest/reference/acm-pca/create-certificate-authority-replica.html) command. The AWS CLI fills in the idempotency token for you.

```
$ aws acm-pca create-certificate-authority-replica \
    --region us-east-1 \
    --certificate-authority-arn arn:aws:acm-pca:us-east-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}} \
    --replica-region us-west-2
```

A replica starts with the status `CREATING` and changes to `ACTIVE` when creation completes. Use the [list-certificate-authority-replicas](https://docs.aws.amazon.com/cli/latest/reference/acm-pca/list-certificate-authority-replicas.html) command to watch the progress from the primary Region.

```
$ aws acm-pca list-certificate-authority-replicas \
    --region us-east-1 \
    --certificate-authority-arn arn:aws:acm-pca:us-east-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}}
```

```json
{
    "Replicas": [
        {
            "Arn": "arn:aws:acm-pca:us-west-2:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}}",
            "Region": "us-west-2",
            "Status": "ACTIVE"
        },
        {
            "Arn": "arn:aws:acm-pca:eu-west-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}}",
            "Region": "eu-west-1",
            "Status": "ACTIVE"
        }
    ]
}
```

## Step 4: Grant access to another account (cross-account only)
<a name="workload-identity-cross-account"></a>

Skip this step if the workload identity provider is in the same AWS account as the CA. If the provider is in another account, grant the actions that IAM needs to the account whose identity attaches the CA. For an organization-level provider, that identity can be in the organization's management account or in a delegated administrator account. Use the [create-permission](https://docs.aws.amazon.com/cli/latest/reference/acm-pca/create-permission.html) command on the primary CA.

```
$ aws acm-pca create-permission \
    --region us-east-1 \
    --certificate-authority-arn arn:aws:acm-pca:us-east-1:{{123456789012}}:certificate-authority/{{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}} \
    --principal arn:aws:iam::{{444455556666}}:root \
    --actions CreateCertificateAuthorityAssociation DeleteCertificateAuthorityAssociation DescribeCertificateAuthority GetCertificateAuthorityCertificate
```

Replace {{444455556666}} with the ID of the account whose identity attaches the CA.

You manage permissions on the primary CA only. They apply to the primary CA and to every replica. On a service-affiliated CA, the principal must be the root principal of another AWS account, and you can grant only the four actions shown in the preceding command.

If the provider is in another account, it must be an organization-level provider. For the requirements when your workloads run in a different account from the CA, see [Cross-account use](PcaWorkloadIdentity-considerations.md#workload-identity-considerations-cross-account).

## Step 5: Attach the CA to the workload identity provider
<a name="workload-identity-attach-signer"></a>

In IAM, attach the CA to the workload identity provider as the X.509 outbound signer of the provider. IAM identifies the CA by the account ID of the owner and the CA ID, not by ARN, because the ARN is specific to a Region and the provider isn't.

```
$ aws iam create-workload-identity-provider-outbound-signer \
    --workload-identity-provider-arn arn:aws:iam::{{123456789012}}:workload-identity-provider/{{my-provider}} \
    --outbound-signer 'X509={CertificateAuthorityId={{a1b2c3d4-5678-90ab-cdef-EXAMPLE11111}},AccountId={{123456789012}}}'
```

IAM then creates the association between the CA and the workload identity provider. For the permissions that attaching the CA needs, see [Prerequisites](#workload-identity-prerequisites).

By default, IAM starts the signer one hour after you attach the CA. The `--signing-delay-in-hours` option changes the delay. Until the signer starts, AWS STS returns `RegionalSignerUnavailableException` to workloads that request certificates.

After the signer starts, your workloads call the AWS STS `GetCertificate` operation at the AWS STS endpoint of their Region. AWS STS issues them a certificate from the CA in that Region. The IAM role that a workload uses needs permission for the `sts:GetCertificate` action.

For information about viewing, rotating, disabling, enabling, and deleting the signer, see [Agent and workload identity](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) in the *IAM User Guide*.

## Next steps
<a name="workload-identity-next-steps"></a>

To do the same with the AWS SDK for Java 2.x, see [Create and activate a CA for AWS IAM for agents and workloads](JavaApiWI-ActivateCA.md). To remove the CA, see [Tear down a CA for AWS IAM for agents and workloads](PcaWorkloadIdentity-teardown.md).