這是 AWS CDK v2 開發人員指南。較舊的 CDK v1 已於 2022 年 6 月 1 日進入維護,並於 2023 年 6 月 1 日結束支援。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
從環境變數取得值
若要取得環境變數的值,請使用如下所示的程式碼。此程式碼會取得環境變數 的值amzn-s3-demo-bucket
。
- TypeScript
-
// Sets bucket_name to undefined if environment variable not set var bucket_name = process.env.amzn-s3-demo-bucket; // Sets bucket_name to a default if env var doesn't exist var bucket_name = process.env.amzn-s3-demo-bucket || "DefaultName";
- JavaScript
-
// Sets bucket_name to undefined if environment variable not set var bucket_name = process.env.amzn-s3-demo-bucket; // Sets bucket_name to a default if env var doesn't exist var bucket_name = process.env.amzn-s3-demo-bucket || "DefaultName";
- Python
-
import os # Raises KeyError if environment variable doesn't exist bucket_name = os.environ["amzn-s3-demo-bucket"] # Sets bucket_name to None if environment variable doesn't exist bucket_name = os.getenv("amzn-s3-demo-bucket") # Sets bucket_name to a default if env var doesn't exist bucket_name = os.getenv("amzn-s3-demo-bucket", "DefaultName")
- Java
-
// Sets bucketName to null if environment variable doesn't exist String bucketName = System.getenv("amzn-s3-demo-bucket"); // Sets bucketName to a default if env var doesn't exist String bucketName = System.getenv("amzn-s3-demo-bucket"); if (bucketName == null) bucketName = "DefaultName";
- C#
-
using System; // Sets bucket name to null if environment variable doesn't exist string bucketName = Environment.GetEnvironmentVariable("amzn-s3-demo-bucket"); // Sets bucket_name to a default if env var doesn't exist string bucketName = Environment.GetEnvironmentVariable("amzn-s3-demo-bucket") ?? "DefaultName";
自訂建構
使用 CloudFormation 參數