ACM使用示例 AWS CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

ACM使用示例 AWS CLI

以下代码示例向您展示了如何使用with来执行操作和实现常见场景ACM。 AWS Command Line Interface

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示如何使用 add-tags-to-certificate

AWS CLI

为现有ACM证书添加标签

以下 add-tags-to-certificate 命令向指定证书添加两个标签。使用空格分隔多个标签:

aws acm add-tags-to-certificate --certificate-arn arn:aws:acm:region:account:certificate/12345678-1234-1234-1234-123456789012 --tags Key=Admin,Value=Alice Key=Purpose,Value=Website

以下代码示例演示如何使用 delete-certificate

AWS CLI

从您的账户中删除ACM证书

以下delete-certificate命令删除具有指定内容的证书ARN:

aws acm delete-certificate --certificate-arn arn:aws:acm:region:account:certificate/12345678-1234-1234-1234-123456789012

以下代码示例演示如何使用 describe-certificate

AWS CLI

检索ACM证书中包含的字段

以下describe-certificate命令检索具有指定值ARN的证书的所有字段:

aws acm describe-certificate --certificate-arn arn:aws:acm:region:account:certificate/12345678-1234-1234-1234-123456789012

输出类似于以下内容:

{ "Certificate": { "CertificateArn": "arn:aws:acm:region:account:certificate/12345678-1234-1234-1234-123456789012", "CreatedAt": 1446835267.0, "DomainName": "www.example.com", "DomainValidationOptions": [ { "DomainName": "www.example.com", "ValidationDomain": "www.example.com", "ValidationEmails": [ "hostmaster@example.com", "admin@example.com", "owner@example.com.whoisprivacyservice.org", "tech@example.com.whoisprivacyservice.org", "admin@example.com.whoisprivacyservice.org", "postmaster@example.com", "webmaster@example.com", "administrator@example.com" ] }, { "DomainName": "www.example.net", "ValidationDomain": "www.example.net", "ValidationEmails": [ "postmaster@example.net", "admin@example.net", "owner@example.net.whoisprivacyservice.org", "tech@example.net.whoisprivacyservice.org", "admin@example.net.whoisprivacyservice.org", "hostmaster@example.net", "administrator@example.net", "webmaster@example.net" ] } ], "InUseBy": [], "IssuedAt": 1446835815.0, "Issuer": "Amazon", "KeyAlgorithm": "RSA-2048", "NotAfter": 1478433600.0, "NotBefore": 1446768000.0, "Serial": "0f:ac:b0:a3:8d:ea:65:52:2d:7d:01:3a:39:36:db:d6", "SignatureAlgorithm": "SHA256WITHRSA", "Status": "ISSUED", "Subject": "CN=www.example.com", "SubjectAlternativeNames": [ "www.example.com", "www.example.net" ] } }

以下代码示例演示如何使用 export-certificate

AWS CLI

导出私有 CA 颁发的私有证书。

以下export-certificate命令将私有证书、证书链和私钥导出到您的显示屏上:

aws acm export-certificate --certificate-arn arn:aws:acm:region:account:certificate/12345678-1234-1234-1234-123456789012 --passphrase file://path-to-passphrase-file

要将证书、证书链和私钥导出到本地文件,请使用以下命令:

aws acm export-certificate --certificate-arn arn:aws:acm:region:sccount:certificate/12345678-1234-1234-1234-123456789012 --passphrase file://path-to-passphrase-file > c:\temp\export.txt

以下代码示例演示如何使用 get-certificate

AWS CLI

检索ACM证书

以下get-certificate命令检索指定证书ARN和证书链的证书:

aws acm get-certificate --certificate-arn arn:aws:acm:region:account:certificate/12345678-1234-1234-1234-123456789012

输出类似于以下内容:

{ "Certificate": "-----BEGIN CERTIFICATE----- MIICiTCCAfICCQD6m7oRw0uXOjANBgkqhkiG9w0BAQUFADCBiDELMAkGA1UEBhMC VVMxCzAJBgNVBAgTAldBMRAwDgYDVQQHEwdTZWF0dGxlMQ8wDQYDVQQKEwZBbWF6 b24xFDASBgNVBAsTC0lBTSBDb25zb2xlMRIwEAYDVQQDEwlUZXN0Q2lsYWMxHzAd BgkqhkiG9w0BCQEWEG5vb25lQGFtYXpvbi5jb20wHhcNMTEwNDI1MjA0NTIxWhcN MTIwNDI0MjA0NTIxWjCBiDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAldBMRAwDgYD VQQHEwd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BjvSfpJIlJ00zbhNYS5f6GuoEDmFJl0ZxBHjJnyp378OD8uTs7fLvjx79LjSTb NYiytVbZPQUQ5Yaxu2jXnimvw3rrszlaEXAMPLE= -----END CERTIFICATE-----" }

以下代码示例演示如何使用 import-certificate

AWS CLI

将证书导入ACM。

以下import-certificate命令将证书导入到ACM。将文件名替换为您自己的文件名:

aws acm import-certificate --certificate file://Certificate.pem --certificate-chain file://CertificateChain.pem --private-key file://PrivateKey.pem

以下代码示例演示如何使用 list-certificates

AWS CLI

列出 AWS 账户的ACM证书

以下list-certificates命令列ARNs出了您账户中的证书:

aws acm list-certificates

上述命令生成类似于以下内容的输出:

{ "CertificateSummaryList": [ { "CertificateArn": "arn:aws:acm:region:account:certificate/12345678-1234-1234-1234-123456789012", "DomainName": "www.example.com" }, { "CertificateArn": "arn:aws:acm:region:account:certificate/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", "DomainName": "www.example.net" } ] }

您可以决定每次调用 list-certificates 时要显示多少证书。例如,如果您有四个证书,且希望每次显示的证书不超过两个,请将 max-items 参数设置为 2,如下例所示:

aws acm list-certificates --max-items 2

将显示两个证书ARNs和一个NextToken值:

"CertificateSummaryList": [ { "CertificateArn": "arn:aws:acm:region:account: \ certificate/12345678-1234-1234-1234-123456789012", "DomainName": "www.example.com" }, { "CertificateArn": "arn:aws:acm:region:account: \ certificate/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", "DomainName": "www.example.net" } ], "NextToken": "9f4d9f69-275a-41fe-b58e-2b837bd9ba48"

要在您的账户中显示接下来的两个证书,请在下次调用中设置此 NextToken 值:

aws acm list-certificates --max-items 2 --next-token 9f4d9f69-275a-41fe-b58e-2b837bd9ba48

您可以使用 certificate-statuses 参数筛选输出。以下命令显示VALIDATION状态为 PENDING _ 的证书:

aws acm list-certificates --certificate-statuses PENDING_VALIDATION

您也可以使用 includes 参数筛选输出。以下命令显示根据以下属性筛选的证书。要显示的证书:

- Specify that the RSA algorithm and a 2048 bit key are used to generate key pairs. - Contain a Key Usage extension that specifies that the certificates can be used to create digital signatures. - Contain an Extended Key Usage extension that specifies that the certificates can be used for code signing. aws acm list-certificates --max-items 10 --includes extendedKeyUsage=CODE_SIGNING,keyUsage=DIGITAL_SIGNATURE,keyTypes=RSA_2048

以下代码示例演示如何使用 list-tags-for-certificate

AWS CLI

列出应用于ACM证书的标签

以下 list-tags-for-certificate 命令列出了应用于您账户中证书的标签:

aws acm list-tags-for-certificate --certificate-arn arn:aws:acm:region:account:certificate/12345678-1234-1234-1234-123456789012

上述命令生成类似于以下内容的输出:

{ "Tags": [ { "Value": "Website", "Key": "Purpose" }, { "Value": "Alice", "Key": "Admin" } ] }

以下代码示例演示如何使用 remove-tags-from-certificate

AWS CLI

从ACM证书中删除标签

以下 remove-tags-from-certificate 命令删除了指定证书的两个标签。使用空格分隔多个标签:

aws acm remove-tags-from-certificate --certificate-arn arn:aws:acm:region:account:certificate/12345678-1234-1234-1234-123456789012 --tags Key=Admin,Value=Alice Key=Purpose,Value=Website

以下代码示例演示如何使用 request-certificate

AWS CLI

申请新ACM证书

以下request-certificate命令使用验证请求 www.example.com 域名的新证书:DNS

aws acm request-certificate --domain-name www.example.com --validation-method DNS

您可以输入幂等性令牌,以区分对 request-certificate 的调用:

aws acm request-certificate --domain-name www.example.com --validation-method DNS --idempotency-token 91adc45q

您可以输入一个或多个主题备用名称来请求能够保护多个顶级域的证书:

aws acm request-certificate --domain-name example.com --validation-method DNS --idempotency-token 91adc45q --subject-alternative-names www.example.net

您可以输入一个备用名称,该名称也可用于访问您的网站:

aws acm request-certificate --domain-name example.com --validation-method DNS --idempotency-token 91adc45q --subject-alternative-names www.example.com

您可以使用星号(*)作为通配符为同一域中的多个子域创建证书:

aws acm request-certificate --domain-name example.com --validation-method DNS --idempotency-token 91adc45q --subject-alternative-names *.example.com

您也可以输入多个备用名称:

aws acm request-certificate --domain-name example.com --validation-method DNS --subject-alternative-names b.example.com c.example.com d.example.com

如果您使用电子邮件进行验证,则可以输入域验证选项来指定将验证电子邮件发送到的域:

aws acm request-certificate --domain-name example.com --validation-method EMAIL --subject-alternative-names www.example.com --domain-validation-options DomainName=example.com,ValidationDomain=example.com

以下命令会在您请求新证书时选择退出证书透明度日志:

aws acm request-certificate --domain-name www.example.com --validation-method DNS --options CertificateTransparencyLoggingPreference=DISABLED --idempotency-token 184627

以下代码示例演示如何使用 resend-validation-email

AWS CLI

为您的ACM证书申请重新发送验证电子邮件

以下 resend-validation-email 命令指示 Amazon 证书颁发机构向相应的地址发送验证电子邮件:

aws acm resend-validation-email --certificate-arn arn:aws:acm:region:account:certificate/12345678-1234-1234-1234-123456789012 --domain www.example.com --validation-domain example.com

以下代码示例演示如何使用 update-certificate-options

AWS CLI

更新证书选项

以下update-certificate-options命令可选择退出证书透明度日志:

aws acm update-certificate-options --certificate-arn arn:aws:acm:region:account:certificate/12345678-1234-1234-1234-123456789012 --options CertificateTransparencyLoggingPreference=DISABLED