쿠키 기본 설정 선택

당사는 사이트와 서비스를 제공하는 데 필요한 필수 쿠키 및 유사한 도구를 사용합니다. 고객이 사이트를 어떻게 사용하는지 파악하고 개선할 수 있도록 성능 쿠키를 사용해 익명의 통계를 수집합니다. 필수 쿠키는 비활성화할 수 없지만 '사용자 지정' 또는 ‘거부’를 클릭하여 성능 쿠키를 거부할 수 있습니다.

사용자가 동의하는 경우 AWS와 승인된 제3자도 쿠키를 사용하여 유용한 사이트 기능을 제공하고, 사용자의 기본 설정을 기억하고, 관련 광고를 비롯한 관련 콘텐츠를 표시합니다. 필수가 아닌 모든 쿠키를 수락하거나 거부하려면 ‘수락’ 또는 ‘거부’를 클릭하세요. 더 자세한 내용을 선택하려면 ‘사용자 정의’를 클릭하세요.

토큰 및 AWS CDK

포커스 모드
토큰 및 AWS CDK - AWS Cloud Development Kit (AWS CDK) v2

v AWS CDK 2 개발자 안내서입니다. 이전 CDK v1은 2022년 6월 1일에 유지 관리에 들어갔으며 2023년 6월 1일에 지원이 종료되었습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

v AWS CDK 2 개발자 안내서입니다. 이전 CDK v1은 2022년 6월 1일에 유지 관리에 들어갔으며 2023년 6월 1일에 지원이 종료되었습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

에서 AWS Cloud Development Kit (AWS CDK)토큰은 구문을 정의하거나 스택을 합성할 때 알려지지 않은 값의 자리 표시자입니다. 이러한 값은 실제 인프라가 생성될 때 배포 시 완전히 해결됩니다. AWS CDK 애플리케이션을 개발할 때 토큰을 사용하여 애플리케이션 전체에서 이러한 값을 관리합니다.

토큰 예

다음은 Amazon Simple Storage Service(Amazon S3) 버킷의 구문을 정의하는 CDK 스택의 예입니다. 버킷의 이름은 아직 알 수 없으므로 bucketName의 값은 토큰으로 저장됩니다.

TypeScript
import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as s3 from 'aws-cdk-lib/aws-s3'; export class CdkDemoAppStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Define an S3 bucket const myBucket = new s3.Bucket(this, 'myBucket'); // Store value of the S3 bucket name const myBucketName = myBucket.bucketName; // Print the current value for the S3 bucket name at synthesis console.log("myBucketName: " + bucketName); } }
JavaScript
const { Stack, Duration } = require('aws-cdk-lib'); const s3 = require('aws-cdk-lib/aws-s3'); class CdkDemoAppStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // Define an S3 bucket const myBucket = new s3.Bucket(this, 'myBucket'); // Store value of the S3 bucket name const myBucketName = myBucket.bucketName; // Print the current value for the S3 bucket name at synthesis console.log("myBucketName: " + myBucketName); } } module.exports = { CdkDemoAppStack }
Python
from aws_cdk import ( Stack ) from constructs import Construct from aws_cdk import aws_s3 as s3 class CdkDemoAppStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # Define an S3 bucket my_bucket = s3.Bucket(self, "myBucket") # Store the value of the S3 bucket name my_bucket_name = my_bucket.bucket_name # Print the current value for the S3 bucket name at synthesis print(f"myBucketName: {my_bucket_name}")
Java
package com.myorg; import software.constructs.Construct; import software.amazon.awscdk.Stack; import software.amazon.awscdk.StackProps; import software.amazon.awscdk.services.s3.Bucket; import java.util.Map; public class CdkDemoAppStack extends Stack { public CdkDemoAppStack(final Construct scope, final String id) { this(scope, id, null); } public CdkDemoAppStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // Define an S3 bucket Bucket myBucket = Bucket.Builder.create(this, "myBucket") .build(); // Store the token for the bucket name String myBucketName = myBucket.getBucketName(); // Print the token at synthesis System.out.println("myBucketName: " + myBucketName); } }
C#
using Amazon.CDK; using Constructs; using Amazon.CDK.AWS.S3; namespace CdkDemoApp { public class CdkDemoAppStack : Stack { internal CdkDemoAppStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // Define an S3 bucket var myBucket = new Bucket(this, "myBucket"); // Store the token for the bucket name var myBucketName = myBucket.BucketName; // Print the token at synthesis System.Console.WriteLine($"myBucketName: {myBucketName}"); } } }
Go
package main import ( "fmt" "github.com/aws/aws-cdk-go/awscdk/v2" "github.com/aws/aws-cdk-go/awscdk/v2/awss3" "github.com/aws/constructs-go/constructs/v10" "github.com/aws/jsii-runtime-go" ) type CdkDemoAppStackProps struct { awscdk.StackProps } func NewCdkDemoAppStack(scope constructs.Construct, id string, props *CdkDemoAppStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) // Define an S3 bucket myBucket := awss3.NewBucket(stack, jsii.String("myBucket"), &awss3.BucketProps{}) // Store the token for the bucket name myBucketName := myBucket.BucketName() // Print the token at synthesis fmt.Println("myBucketName: ", *myBucketName) return stack } // ...
import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as s3 from 'aws-cdk-lib/aws-s3'; export class CdkDemoAppStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Define an S3 bucket const myBucket = new s3.Bucket(this, 'myBucket'); // Store value of the S3 bucket name const myBucketName = myBucket.bucketName; // Print the current value for the S3 bucket name at synthesis console.log("myBucketName: " + bucketName); } }

스택을 합성하기 위해 cdk synth를 실행하면 myBucketName의 값이 ${Token[TOKEN.1234]} 토큰 형식으로 표시됩니다. 이 토큰 형식은가 토큰을 AWS CDK 인코딩하는 방식의 결과입니다. 이 예에서는 토큰이 문자열로 인코딩됩니다.

$ cdk synth --quiet myBucketName: ${Token[TOKEN.21]}

합성 시 버킷 이름의 값을 알 수 없으므로 토큰은 myBucket<unique-hash>로 렌더링됩니다. AWS CloudFormation 템플릿은 Ref 내장 함수를 사용하여 배포 시 알려진 값을 참조합니다.

Resources: myBucket5AF9C99B: # ... Outputs: bucketNameOutput: Description: The name of the S3 bucket Value: Ref: myBucket5AF9C99B

고유한 해시가 생성되는 방법에 대한 자세한 내용은 AWS CloudFormation 템플릿에서 생성된 논리적 IDs 섹션을 참조하세요.

토큰 전달

토큰은 마치 토큰이 나타내는 실제 값인 것처럼 전달될 수 있습니다. 다음은 버킷 이름의 토큰을 AWS Lambda 함수의 구문에 전달하는 예제입니다.

TypeScript

import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as s3 from 'aws-cdk-lib/aws-s3'; import * as lambda from 'aws-cdk-lib/aws-lambda'; export class CdkDemoAppStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Define an S3 bucket const myBucket = new s3.Bucket(this, 'myBucket'); // ... // Define a Lambda function const myFunction = new lambda.Function(this, "myFunction", { runtime: lambda.Runtime.NODEJS_20_X, handler: "index.handler", code: lambda.Code.fromInline(` exports.handler = async function(event) { return { statusCode: 200, body: JSON.stringify('Hello World!'), }; }; `), functionName: myBucketName + "Function", // Pass token for the S3 bucket name environment: { BUCKET_NAME: myBucketName, // Pass token for the S3 bucket name } }); } }
JavaScript
const { Stack, Duration } = require('aws-cdk-lib'); const s3 = require('aws-cdk-lib/aws-s3'); const lambda = require('aws-cdk-lib/aws-lambda'); class CdkDemoAppStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // Define an S3 bucket const myBucket = new s3.Bucket(this, 'myBucket'); // ... // Define a Lambda function const myFunction = new lambda.Function(this, 'myFunction', { runtime: lambda.Runtime.NODEJS_20_X, handler: 'index.handler', code: lambda.Code.fromInline(` exports.handler = async function(event) { return { statusCode: 200, body: JSON.stringify('Hello World!'), }; }; `), functionName: myBucketName + 'Function', // Pass token for the S3 bucket name environment: { BUCKET_NAME: myBucketName, // Pass token for the S3 bucket name } }); } } module.exports = { CdkDemoAppStack }
Python
from aws_cdk import ( Stack ) from constructs import Construct from aws_cdk import aws_s3 as s3 from aws_cdk import aws_lambda as _lambda class CdkDemoAppStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # Define an S3 bucket my_bucket = s3.Bucket(self, "myBucket") # ... # Define a Lambda function my_function = _lambda.Function(self, "myFunction", runtime=_lambda.Runtime.NODEJS_20_X, handler="index.handler", code=_lambda.Code.from_inline(""" exports.handler = async function(event) { return { statusCode: 200, body: JSON.stringify('Hello World!'), }; }; """), function_name=f"{my_bucket_name}Function", # Pass token for the S3 bucket name environment={ "BUCKET_NAME": my_bucket_name # Pass token for the S3 bucket name } )
Java
package com.myorg; import software.constructs.Construct; import software.amazon.awscdk.Stack; import software.amazon.awscdk.StackProps; import software.amazon.awscdk.services.s3.Bucket; import software.amazon.awscdk.services.lambda.Code; import software.amazon.awscdk.services.lambda.Function; import software.amazon.awscdk.services.lambda.Runtime; import java.util.Map; public class CdkDemoAppStack extends Stack { public CdkDemoAppStack(final Construct scope, final String id) { this(scope, id, null); } public CdkDemoAppStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // Define an S3 bucket Bucket myBucket = Bucket.Builder.create(this, "myBucket") .build(); // ... // Define a Lambda function Function myFunction = Function.Builder.create(this, "myFunction") .runtime(Runtime.NODEJS_20_X) .handler("index.handler") .code(Code.fromInline( "exports.handler = async function(event) {" + "return {" + "statusCode: 200," + "body: JSON.stringify('Hello World!')," + "};" + "};" )) .functionName(myBucketName + "Function") // Pass the token for the s3 bucket to the function construct .environment(Map.of("BUCKET_NAME", myBucketName)) // Pass the bucket name as environment variable .build(); } }
C#
using Amazon.CDK; using Constructs; using Amazon.CDK.AWS.S3; using Amazon.CDK.AWS.Lambda; using System; using System.Collections.Generic; namespace CdkDemoApp { public class CdkDemoAppStack : Stack { internal CdkDemoAppStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // Define an S3 bucket var myBucket = new Bucket(this, "myBucket"); // ... // Define a Lambda function var myFunction = new Function(this, "myFunction", new FunctionProps { Runtime = Runtime.NODEJS_20_X, Handler = "index.handler", Code = Code.FromInline(@" exports.handler = async function(event) { return { statusCode: 200, body: JSON.stringify('Hello World!'), }; }; "), // Pass the token for the S3 bucket name Environment = new Dictionary<string, string> { { "BUCKET_NAME", myBucketName } }, FunctionName = $"{myBucketName}Function" // Pass the token for the s3 bucket to the function construct }); } } }
Go
package main import ( "fmt" "github.com/aws/aws-cdk-go/awscdk/v2" "github.com/aws/aws-cdk-go/awscdk/v2/awslambda" "github.com/aws/aws-cdk-go/awscdk/v2/awss3" "github.com/aws/constructs-go/constructs/v10" "github.com/aws/jsii-runtime-go" ) type CdkDemoAppStackProps struct { awscdk.StackProps } func NewCdkDemoAppStack(scope constructs.Construct, id string, props *CdkDemoAppStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) // Define an S3 bucket myBucket := awss3.NewBucket(stack, jsii.String("myBucket"), &awss3.BucketProps{}) // ... // Define a Lambda function myFunction := awslambda.NewFunction(stack, jsii.String("myFunction"), &awslambda.FunctionProps{ Runtime: awslambda.Runtime_NODEJS_20_X(), Handler: jsii.String("index.handler"), Code: awslambda.Code_FromInline(jsii.String(` exports.handler = async function(event) { return { statusCode: 200, body: JSON.stringify('Hello World!'), }; }; `)), FunctionName: jsii.String(fmt.Sprintf("%sFunction", *myBucketName)), // Pass the token for the S3 bucket to the function name Environment: &map[string]*string{ "BUCKET_NAME": myBucketName, }, }) return stack } // ...

import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as s3 from 'aws-cdk-lib/aws-s3'; import * as lambda from 'aws-cdk-lib/aws-lambda'; export class CdkDemoAppStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Define an S3 bucket const myBucket = new s3.Bucket(this, 'myBucket'); // ... // Define a Lambda function const myFunction = new lambda.Function(this, "myFunction", { runtime: lambda.Runtime.NODEJS_20_X, handler: "index.handler", code: lambda.Code.fromInline(` exports.handler = async function(event) { return { statusCode: 200, body: JSON.stringify('Hello World!'), }; }; `), functionName: myBucketName + "Function", // Pass token for the S3 bucket name environment: { BUCKET_NAME: myBucketName, // Pass token for the S3 bucket name } }); } }

템플릿을 합성할 때 RefFn::Join 내장 함수를 사용하여 배포 시 알려진 값을 지정합니다.

Resources: myBucket5AF9C99B: Type: AWS::S3::Bucket # ... myFunction884E1557: Type: AWS::Lambda::Function Properties: # ... Environment: Variables: BUCKET_NAME: Ref: myBucket5AF9C99B FunctionName: Fn::Join: - "" - - Ref: myBucket5AF9C99B - Function # ...

토큰 인코딩 작동 방식

토큰은 단일 resolve 메서드를 포함하는 IResolvable 인터페이스를 구현하는 객체입니다. 합성 중에는이 메서드를 AWS CDK 호출하여 CloudFormation 템플릿에서 토큰의 최종 값을 생성합니다.

참고

IResolvable 인터페이스로 직접 작업하는 경우는 거의 없습니다. 문자열로 인코딩된 버전의 토큰만 표시될 가능성이 높습니다.

토큰 인코딩 유형

토큰은 합성 프로세스에 참여하여 모든 유형의 임의 값을 생성합니다. 다른 함수는 일반적으로 string 또는 number와 같은 기본 유형의 인수만 수락합니다. 이러한 경우 토큰을 사용하려면 cdk.Token 클래스에서 정적 메서드를 사용하여 토큰을 세 가지 유형 중 하나로 인코딩할 수 있습니다.

  • Token.asString - 문자열 인코딩을 생성하거나 토큰 객체에서 .toString()을 직접적으로 호출합니다.

  • Token.asList - 목록 인코딩을 생성합니다.

  • Token.asNumber - 숫자 인코딩을 생성합니다.

이들은 임의의 값을 가져와 IResolvable이 될 수 있으며, 표시된 유형의 기본 값으로 인코딩합니다.

중요

이전 유형 중 하나가 인코딩된 토큰일 가능성이 있으므로 콘텐츠를 구문 분석하거나 읽으려고 할 때 주의해야 합니다. 예를 들어 문자열에서 값을 추출하기 위해 문자열을 구문 분석하려고 하는데 문자열이 인코딩된 토큰인 경우 구문 분석이 실패합니다. 마찬가지로 배열 길이를 쿼리하거나 숫자를 사용하여 수학 작업을 수행하려는 경우 먼저 해당 배열이 인코딩된 토큰이 아닌지 확인해야 합니다.

앱에서 토큰을 확인하는 방법

값에 해결되지 않은 토큰이 있는지 확인하려면 Token.isUnresolved(Python: is_unresolved) 메서드를 직접적으로 호출합니다. 다음은 Amazon S3 버킷 이름의 값이 토큰인지 확인하는 예입니다. 토큰이 아닌 경우 버킷 이름의 길이를 확인합니다.

TypeScript

// ... export class CdkDemoAppStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Define an S3 bucket const myBucket = new s3.Bucket(this, 'myBucket'); // ... // Check if bucket name is a token. If not, check if length is less than 10 characters if (cdk.Token.isUnresolved(myBucketName)) { console.log("Token identified."); } else if (!cdk.Token.isUnresolved(myBucketName) && myBucketName.length > 10) { throw new Error('Maximum length for name is 10 characters.'); }; // ...
JavaScript
const { Stack, Duration, Token, CfnOutput } = require('aws-cdk-lib'); // ... class CdkDemoAppStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // Define an S3 bucket const myBucket = new s3.Bucket(this, 'myBucket'); // ... // Check if bucket name is a token. If not, check if length is less than 10 characters if (Token.isUnresolved(myBucketName)) { console.log("Token identified."); } else if (!Token.isUnresolved(myBucketName) && myBucketName.length > 10) { throw new Error('Maximum length for name is 10 characters.'); }; // ...
Python

from aws_cdk import ( Stack, Token ) # ... class CdkDemoAppStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # Define an S3 bucket my_bucket = s3.Bucket(self, "myBucket") # ... # Check if bucket name is a token. If not, check if length is less than 10 characters if Token.is_unresolved(my_bucket_name): print("Token identified.") elif not Token.is_unresolved(my_bucket_name) and len(my_bucket_name) < 10: raise ValueError("Maximum length for name is 10 characters.") # ...
Java
// ... import software.amazon.awscdk.Token; // ... public class CdkDemoAppStack extends Stack { public CdkDemoAppStack(final Construct scope, final String id) { this(scope, id, null); } public CdkDemoAppStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // Define an S3 bucket Bucket myBucket = Bucket.Builder.create(this, "myBucket") .build(); // ... // Check if the bucket name is a token. If not, check if length is less than 10 characters if (Token.isUnresolved(myBucketName)) { System.out.println("Token identified."); } else if (!Token.isUnresolved(myBucketName) && myBucketName.length() > 10) { throw new IllegalArgumentException("Maximum length for name is 10 characters."); } // ... } }
C#
using Amazon.CDK; using Constructs; using Amazon.CDK.AWS.S3; using Amazon.CDK.AWS.Lambda; using System; using System.Collections.Generic; namespace CdkDemoApp { public class CdkDemoAppStack : Stack { internal CdkDemoAppStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // Define an S3 bucket var myBucket = new Bucket(this, "myBucket"); // ... // Check if bucket name is a token. If not, check if length is less than 10 characters if (Token.IsUnresolved(myBucketName)) { System.Console.WriteLine("Token identified."); } else if (!Token.IsUnresolved(myBucketName) && myBucketName.Length > 10) { throw new System.Exception("Maximum length for name is 10 characters."); } // ...
Go
// ... func NewCdkDemoAppStack(scope constructs.Construct, id string, props *CdkDemoAppStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) // Define an S3 bucket myBucket := awss3.NewBucket(stack, jsii.String("myBucket"), &awss3.BucketProps{}) // ... // Check if the bucket name is unresolved (a token) if tokenUnresolved := awscdk.Token_IsUnresolved(myBucketName); tokenUnresolved != nil && *tokenUnresolved { fmt.Println("Token identified.") } else if tokenUnresolved != nil && !*tokenUnresolved && len(*myBucketName) > 10 { panic("Maximum length for name is 10 characters.") } // ...

// ... export class CdkDemoAppStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Define an S3 bucket const myBucket = new s3.Bucket(this, 'myBucket'); // ... // Check if bucket name is a token. If not, check if length is less than 10 characters if (cdk.Token.isUnresolved(myBucketName)) { console.log("Token identified."); } else if (!cdk.Token.isUnresolved(myBucketName) && myBucketName.length > 10) { throw new Error('Maximum length for name is 10 characters.'); }; // ...

cdk synth를 실행하면 myBucketName이 토큰으로 식별됩니다.

$ cdk synth --quiet Token identified.
참고

토큰 인코딩을 사용하여 유형 시스템을 이스케이프할 수 있습니다. 예를 들어 합성 시 숫자 값을 생성하는 토큰을 문자열로 인코딩할 수 있습니다. 이러한 함수를 사용하는 경우 합성 후 템플릿이 사용 가능한 상태로 확인되는지 확인하는 것은 사용자의 책임입니다.

문자열 인코딩 토큰 작업

문자열로 인코딩된 토큰은 다음과 같습니다.

${TOKEN[Bucket.Name.1234]}

다음 예와 같이 일반 문자열처럼 전달하고 연결할 수 있습니다.

TypeScript
const functionName = bucket.bucketName + 'Function';
JavaScript
const functionName = bucket.bucketName + 'Function';
Python
function_name = bucket.bucket_name + "Function"
Java
String functionName = bucket.getBucketName().concat("Function");
C#
string functionName = bucket.BucketName + "Function";
Go
functionName := *bucket.BucketName() + "Function"
const functionName = bucket.bucketName + 'Function';

다음 예와 같이 언어에서 지원하는 경우 문자열 보간을 사용할 수도 있습니다.

TypeScript
const functionName = `${bucket.bucketName}Function`;
JavaScript
const functionName = `${bucket.bucketName}Function`;
Python
function_name = f"{bucket.bucket_name}Function"
Java
String functionName = String.format("%sFunction". bucket.getBucketName());
C#
string functionName = $"${bucket.bucketName}Function";
Go

유사한 기능에 fmt.Sprintf 사용:

functionName := fmt.Sprintf("%sFunction", *bucket.BucketName())
const functionName = `${bucket.bucketName}Function`;

다른 방식으로 문자열을 조작하지 마세요. 예를 들어 문자열의 하위 문자열을 사용하면 문자열 토큰이 파손될 수 있습니다.

목록 인코딩 토큰 작업

목록 인코딩 토큰은 다음과 같습니다.

["#{TOKEN[Stack.NotificationArns.1234]}"]

이러한 목록을 안전하게 사용할 수 있는 유일한 방법은 다른 구문에 직접 전달하는 것입니다. 문자열 목록 형식의 토큰은 연결할 수 없으며 토큰에서 요소를 가져올 수도 없습니다. 이를 처리하는 유일한 안전한 방법은 Fn.select와 같은 AWS CloudFormation 내장 함수를 사용하는 것입니다.

숫자로 인코딩된 토큰 작업

숫자로 인코딩된 토큰은 다음과 같은 작은 음의 부동 소수점 숫자 집합입니다.

-1.8881545897087626e+289

목록 토큰과 마찬가지로 숫자 값을 수정할 수 없습니다. 이렇게 하면 숫자 토큰이 손상될 수 있습니다.

다음은 숫자로 인코딩된 토큰이 포함된 구문의 예입니다.

TypeScript
import { Stack, Duration, StackProps } from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as rds from 'aws-cdk-lib/aws-rds'; import * as ec2 from 'aws-cdk-lib/aws-ec2'; export class CdkDemoAppStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); // Define a new VPC const vpc = new ec2.Vpc(this, 'MyVpc', { maxAzs: 3, // Maximum number of availability zones to use }); // Define an RDS database cluster const dbCluster = new rds.DatabaseCluster(this, 'MyRDSCluster', { engine: rds.DatabaseClusterEngine.AURORA, instanceProps: { vpc, }, }); // Get the port token (this is a token encoded as a number) const portToken = dbCluster.clusterEndpoint.port; // Print the value for our token at synthesis console.log("portToken: " + portToken); } }
JavaScript
const { Stack, Duration } = require('aws-cdk-lib'); const lambda = require('aws-cdk-lib/aws-lambda'); const rds = require('aws-cdk-lib/aws-rds'); const ec2 = require('aws-cdk-lib/aws-ec2'); class CdkDemoAppStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // Define a new VPC const vpc = new ec2.Vpc(this, 'MyVpc', { maxAzs: 3, // Maximum number of availability zones to use }); // Define an RDS database cluster const dbCluster = new rds.DatabaseCluster(this, 'MyRDSCluster', { engine: rds.DatabaseClusterEngine.AURORA, instanceProps: { vpc, }, }); // Get the port token (this is a token encoded as a number) const portToken = dbCluster.clusterEndpoint.port; // Print the value for our token at synthesis console.log("portToken: " + portToken); } } module.exports = { CdkDemoAppStack }
Python
from aws_cdk import ( Duration, Stack, ) from aws_cdk import aws_rds as rds from aws_cdk import aws_ec2 as ec2 from constructs import Construct class CdkDemoAppStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # Define a new VPC vpc = ec2.Vpc(self, 'MyVpc', max_azs=3 # Maximum number of availability zones to use ) # Define an RDS database cluster db_cluster = rds.DatabaseCluster(self, 'MyRDSCluster', engine=rds.DatabaseClusterEngine.AURORA, instance_props=rds.InstanceProps( vpc=vpc ) ) # Get the port token (this is a token encoded as a number) port_token = db_cluster.cluster_endpoint.port # Print the value for our token at synthesis print(f"portToken: {port_token}")
Java
package com.myorg; import software.constructs.Construct; import software.amazon.awscdk.Stack; import software.amazon.awscdk.StackProps; import software.amazon.awscdk.services.ec2.Vpc; import software.amazon.awscdk.services.rds.DatabaseCluster; import software.amazon.awscdk.services.rds.DatabaseClusterEngine; import software.amazon.awscdk.services.rds.InstanceProps; public class CdkDemoAppStack extends Stack { public CdkDemoAppStack(final Construct scope, final String id) { this(scope, id, null); } public CdkDemoAppStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // Define a new VPC Vpc vpc = Vpc.Builder.create(this, "MyVpc") .maxAzs(3) // Maximum number of availability zones to use .build(); // Define an RDS database cluster DatabaseCluster dbCluster = DatabaseCluster.Builder.create(this, "MyRDSCluster") .engine(DatabaseClusterEngine.AURORA) .instanceProps(InstanceProps.builder() .vpc(vpc) .build()) .build(); // Get the port token (this is a token encoded as a number) Number portToken = dbCluster.getClusterEndpoint().getPort(); // Print the value for our token at synthesis System.out.println("portToken: " + portToken); } }
C#
using Amazon.CDK; using Constructs; using Amazon.CDK.AWS.EC2; using Amazon.CDK.AWS.RDS; using System; using System.Collections.Generic; namespace CdkDemoApp { public class CdkDemoAppStack : Stack { internal CdkDemoAppStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // Define a new VPC var vpc = new Vpc(this, "MyVpc", new VpcProps { MaxAzs = 3 // Maximum number of availability zones to use }); // Define an RDS database cluster var dbCluster = new DatabaseCluster(this, "MyRDSCluster", new DatabaseClusterProps { Engine = DatabaseClusterEngine.AURORA, // Remove parentheses InstanceProps = new Amazon.CDK.AWS.RDS.InstanceProps // Specify RDS InstanceProps { Vpc = vpc } }); // Get the port token (this is a token encoded as a number) var portToken = dbCluster.ClusterEndpoint.Port; // Print the value for our token at synthesis System.Console.WriteLine($"portToken: {portToken}"); } } }
Go
package main import ( "fmt" "github.com/aws/aws-cdk-go/awscdk/v2" "github.com/aws/aws-cdk-go/awscdk/v2/awsec2" "github.com/aws/aws-cdk-go/awscdk/v2/awsrds" "github.com/aws/constructs-go/constructs/v10" "github.com/aws/jsii-runtime-go" ) type CdkDemoAppStackProps struct { awscdk.StackProps } func NewCdkDemoAppStack(scope constructs.Construct, id string, props *CdkDemoAppStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) // Define a new VPC vpc := awsec2.NewVpc(stack, jsii.String("MyVpc"), &awsec2.VpcProps{ MaxAzs: jsii.Number(3), // Maximum number of availability zones to use }) // Define an RDS database cluster dbCluster := awsrds.NewDatabaseCluster(stack, jsii.String("MyRDSCluster"), &awsrds.DatabaseClusterProps{ Engine: awsrds.DatabaseClusterEngine_AURORA(), InstanceProps: &awsrds.InstanceProps{ Vpc: vpc, }, }) // Get the port token (this is a token encoded as a number) portToken := dbCluster.ClusterEndpoint().Port() // Print the value for our token at synthesis fmt.Println("portToken: ", portToken) return stack } // ...
import { Stack, Duration, StackProps } from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as rds from 'aws-cdk-lib/aws-rds'; import * as ec2 from 'aws-cdk-lib/aws-ec2'; export class CdkDemoAppStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); // Define a new VPC const vpc = new ec2.Vpc(this, 'MyVpc', { maxAzs: 3, // Maximum number of availability zones to use }); // Define an RDS database cluster const dbCluster = new rds.DatabaseCluster(this, 'MyRDSCluster', { engine: rds.DatabaseClusterEngine.AURORA, instanceProps: { vpc, }, }); // Get the port token (this is a token encoded as a number) const portToken = dbCluster.clusterEndpoint.port; // Print the value for our token at synthesis console.log("portToken: " + portToken); } }

cdk synth를 실행하면 portToken의 값이 숫자로 인코딩된 토큰으로 표시됩니다.

$ cdk synth --quiet portToken: -1.8881545897087968e+289

패스 번호 인코딩 토큰

숫자로 인코딩된 토큰을 다른 구문에 전달할 때는 먼저 문자열로 변환하는 것이 좋습니다. 예를 들어 숫자로 인코딩된 문자열의 값을 연결 문자열의 일부로 사용하려는 경우 변환하면 가독성을 높이는 데 도움이 됩니다.

다음 예에서 portTokenconnectionString의 일부로 Lambda 함수에 전달하려는 숫자로 인코딩된 토큰입니다.

TypeScript
import { Stack, Duration, CfnOutput, StackProps } from 'aws-cdk-lib'; // ... import * as lambda from 'aws-cdk-lib/aws-lambda'; export class CdkDemoAppStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); // Define a new VPC // ... // Define an RDS database cluster // ... // Get the port token (this is a token encoded as a number) const portToken = dbCluster.clusterEndpoint.port; // ... // Example connection string with the port token as a number const connectionString = `jdbc:mysql://mydb.cluster.amazonaws.com:${portToken}/mydatabase`; // Use the connection string as an environment variable in a Lambda function const myFunction = new lambda.Function(this, 'MyLambdaFunction', { runtime: lambda.Runtime.NODEJS_20_X, handler: 'index.handler', code: lambda.Code.fromInline(` exports.handler = async function(event) { return { statusCode: 200, body: JSON.stringify('Hello World!'), }; }; `), environment: { DATABASE_CONNECTION_STRING: connectionString, // Using the port token as part of the string }, }); // Output the value of our connection string at synthesis console.log("connectionString: " + connectionString); // Output the connection string new CfnOutput(this, 'ConnectionString', { value: connectionString, }); } }
JavaScript
const { Stack, Duration, CfnOutput } = require('aws-cdk-lib'); // ... const lambda = require('aws-cdk-lib/aws-lambda'); class CdkDemoAppStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // Define a new VPC // ... // Define an RDS database cluster // ... // Get the port token (this is a token encoded as a number) const portToken = dbCluster.clusterEndpoint.port; // ... // Example connection string with the port token as a number const connectionString = `jdbc:mysql://mydb.cluster.amazonaws.com:${portToken}/mydatabase`; // Use the connection string as an environment variable in a Lambda function const myFunction = new lambda.Function(this, 'MyLambdaFunction', { runtime: lambda.Runtime.NODEJS_20_X, handler: 'index.handler', code: lambda.Code.fromInline(` exports.handler = async function(event) { return { statusCode: 200, body: JSON.stringify('Hello World!'), }; }; `), environment: { DATABASE_CONNECTION_STRING: connectionString, // Using the port token as part of the string }, }); // Output the value of our connection string at synthesis console.log("connectionString: " + connectionString); // Output the connection string new CfnOutput(this, 'ConnectionString', { value: connectionString, }); } } module.exports = { CdkDemoAppStack }
Python
from aws_cdk import ( Duration, Stack, CfnOutput, ) from aws_cdk import aws_lambda as _lambda # ... class CdkDemoAppStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # Define a new VPC # ... # Define an RDS database cluster # ... # Get the port token (this is a token encoded as a number) port_token = db_cluster.cluster_endpoint.port # ... # Example connection string with the port token as a number connection_string = f"jdbc:mysql://mydb.cluster.amazonaws.com:{port_token}/mydatabase" # Use the connection string as an environment variable in a Lambda function my_function = _lambda.Function(self, 'MyLambdaFunction', runtime=_lambda.Runtime.NODEJS_20_X, handler='index.handler', code=_lambda.Code.from_inline(""" exports.handler = async function(event) { return { statusCode: 200, body: JSON.stringify('Hello World!'), }; }; """), environment={ 'DATABASE_CONNECTION_STRING': connection_string # Using the port token as part of the string } ) # Output the value of our connection string at synthesis print(f"connectionString: {connection_string}") # Output the connection string CfnOutput(self, 'ConnectionString', value=connection_string )
Java

// ... import software.amazon.awscdk.CfnOutput; import software.amazon.awscdk.services.lambda.Function; import software.amazon.awscdk.services.lambda.Runtime; import software.amazon.awscdk.services.lambda.Code; import java.util.Map; public class CdkDemoAppStack extends Stack { public CdkDemoAppStack(final Construct scope, final String id) { this(scope, id, null); } public CdkDemoAppStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // Define a new VPC // ... // Define an RDS database cluster // ... // Get the port token (this is a token encoded as a number) Number portToken = dbCluster.getClusterEndpoint().getPort(); // ... // Example connection string with the port token as a number String connectionString = "jdbc:mysql://mydb.cluster.amazonaws.com:" + portToken + "/mydatabase"; // Use the connection string as an environment variable in a Lambda function Function myFunction = Function.Builder.create(this, "MyLambdaFunction") .runtime(Runtime.NODEJS_20_X) .handler("index.handler") .code(Code.fromInline( "exports.handler = async function(event) {\n" + " return {\n" + " statusCode: 200,\n" + " body: JSON.stringify('Hello World!'),\n" + " };\n" + "};")) .environment(Map.of( "DATABASE_CONNECTION_STRING", connectionString // Using the port token as part of the string )) .build(); // Output the value of our connection string at synthesis System.out.println("connectionString: " + connectionString); // Output the connection string CfnOutput.Builder.create(this, "ConnectionString") .value(connectionString) .build(); } }
C#
// ... using Amazon.CDK.AWS.Lambda; namespace CdkDemoApp { public class CdkDemoAppStack : Stack { internal CdkDemoAppStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // Define a new VPC // ... // Define an RDS database cluster var dbCluster = new DatabaseCluster(this, "MyRDSCluster", new DatabaseClusterProps // ... // Get the port token (this is a token encoded as a number) var portToken = dbCluster.ClusterEndpoint.Port; // ... // Example connection string with the port token as a number var connectionString = $"jdbc:mysql://mydb.cluster.amazonaws.com:{portToken}/mydatabase"; // Use the connection string as an environment variable in a Lambda function var myFunction = new Function(this, "MyLambdaFunction", new FunctionProps { Runtime = Runtime.NODEJS_20_X, Handler = "index.handler", Code = Code.FromInline(@" exports.handler = async function(event) { return { statusCode: 200, body: JSON.stringify('Hello World!'), }; }; "), Environment = new Dictionary<string, string> { { "DATABASE_CONNECTION_STRING", connectionString } // Using the port token as part of the string } }); // Output the value of our connection string at synthesis Console.WriteLine($"connectionString: {connectionString}"); // Output the connection string new CfnOutput(this, "ConnectionString", new CfnOutputProps { Value = connectionString }); } } }
Go
// ... "github.com/aws/aws-cdk-go/awscdk/v2/awslambda" ) type CdkDemoAppStackProps struct { awscdk.StackProps } func NewCdkDemoAppStack(scope constructs.Construct, id string, props *CdkDemoAppStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) // Define a new VPC // ... // Define an RDS database cluster // ... // Get the port token (this is a token encoded as a number) portToken := dbCluster.ClusterEndpoint().Port() // ... // Example connection string with the port token as a number connectionString := fmt.Sprintf("jdbc:mysql://mydb.cluster.amazonaws.com:%s/mydatabase", portToken) // Use the connection string as an environment variable in a Lambda function myFunction := awslambda.NewFunction(stack, jsii.String("MyLambdaFunction"), &awslambda.FunctionProps{ Runtime: awslambda.Runtime_NODEJS_20_X(), Handler: jsii.String("index.handler"), Code: awslambda.Code_FromInline(jsii.String(` exports.handler = async function(event) { return { statusCode: 200, body: JSON.stringify('Hello World!'), }; }; `)), Environment: &map[string]*string{ "DATABASE_CONNECTION_STRING": jsii.String(connectionString), // Using the port token as part of the string }, }) // Output the value of our connection string at synthesis fmt.Println("connectionString: ", connectionString) // Output the connection string awscdk.NewCfnOutput(stack, jsii.String("ConnectionString"), &awscdk.CfnOutputProps{ Value: jsii.String(connectionString), }) return stack } // ...
import { Stack, Duration, CfnOutput, StackProps } from 'aws-cdk-lib'; // ... import * as lambda from 'aws-cdk-lib/aws-lambda'; export class CdkDemoAppStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); // Define a new VPC // ... // Define an RDS database cluster // ... // Get the port token (this is a token encoded as a number) const portToken = dbCluster.clusterEndpoint.port; // ... // Example connection string with the port token as a number const connectionString = `jdbc:mysql://mydb.cluster.amazonaws.com:${portToken}/mydatabase`; // Use the connection string as an environment variable in a Lambda function const myFunction = new lambda.Function(this, 'MyLambdaFunction', { runtime: lambda.Runtime.NODEJS_20_X, handler: 'index.handler', code: lambda.Code.fromInline(` exports.handler = async function(event) { return { statusCode: 200, body: JSON.stringify('Hello World!'), }; }; `), environment: { DATABASE_CONNECTION_STRING: connectionString, // Using the port token as part of the string }, }); // Output the value of our connection string at synthesis console.log("connectionString: " + connectionString); // Output the connection string new CfnOutput(this, 'ConnectionString', { value: connectionString, }); } }

이 값을 connectionString에 전달하면 cdk synth 실행 시 출력 값이 숫자로 인코딩된 문자열로 인해 혼동될 수 있습니다.

$ cdk synth --quiet connectionString: jdbc:mysql://mydb.cluster.amazonaws.com:-1.888154589708796e+289/mydatabase

숫자로 인코딩된 토큰을 문자열로 변환하려면 cdk.Tokenization.stringifyNumber(token)를 사용합니다. 다음 예에서는 연결 문자열을 정의하기 전에 숫자로 인코딩된 토큰을 문자열로 변환합니다.

TypeScript
import { Stack, Duration, Tokenization, CfnOutput, StackProps } from 'aws-cdk-lib'; // ... export class CdkDemoAppStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); // Define a new VPC // ... // Define an RDS database cluster // ... // Get the port token (this is a token encoded as a number) const portToken = dbCluster.clusterEndpoint.port; // ... // Convert the encoded number to an encoded string for use in the connection string const portAsString = Tokenization.stringifyNumber(portToken); // Example connection string with the port token as a string const connectionString = `jdbc:mysql://mydb.cluster.amazonaws.com:${portAsString}/mydatabase`; // Use the connection string as an environment variable in a Lambda function const myFunction = new lambda.Function(this, 'MyLambdaFunction', { // ... environment: { DATABASE_CONNECTION_STRING: connectionString, // Using the port token as part of the string }, }); // Output the value of our connection string at synthesis console.log("connectionString: " + connectionString); // Output the connection string new CfnOutput(this, 'ConnectionString', { value: connectionString, }); } }
JavaScript
const { Stack, Duration, Tokenization, CfnOutput } = require('aws-cdk-lib'); // ... class CdkDemoAppStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // Define a new VPC // ... // Define an RDS database cluster // ... // Get the port token (this is a token encoded as a number) const portToken = dbCluster.clusterEndpoint.port; // ... // Convert the encoded number to an encoded string for use in the connection string const portAsString = Tokenization.stringifyNumber(portToken); // Example connection string with the port token as a string const connectionString = `jdbc:mysql://mydb.cluster.amazonaws.com:${portAsString}/mydatabase`; // Use the connection string as an environment variable in a Lambda function const myFunction = new lambda.Function(this, 'MyLambdaFunction', { // ... environment: { DATABASE_CONNECTION_STRING: connectionString, // Using the port token as part of the string }, }); // Output the value of our connection string at synthesis console.log("connectionString: " + connectionString); // Output the connection string new CfnOutput(this, 'ConnectionString', { value: connectionString, }); } } module.exports = { CdkDemoAppStack }
Python
from aws_cdk import ( Duration, Stack, Tokenization, CfnOutput, ) # ... class CdkDemoAppStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # Define a new VPC # ... # Define an RDS database cluster # ... # Get the port token (this is a token encoded as a number) port_token = db_cluster.cluster_endpoint.port # Convert the encoded number to an encoded string for use in the connection string port_as_string = Tokenization.stringify_number(port_token) # Example connection string with the port token as a string connection_string = f"jdbc:mysql://mydb.cluster.amazonaws.com:{port_as_string}/mydatabase" # Use the connection string as an environment variable in a Lambda function my_function = _lambda.Function(self, 'MyLambdaFunction', # ... environment={ 'DATABASE_CONNECTION_STRING': connection_string # Using the port token as part of the string } ) # Output the value of our connection string at synthesis print(f"connectionString: {connection_string}") # Output the connection string CfnOutput(self, 'ConnectionString', value=connection_string )
Java
// ... import software.amazon.awscdk.Tokenization; public class CdkDemoAppStack extends Stack { public CdkDemoAppStack(final Construct scope, final String id) { this(scope, id, null); } public CdkDemoAppStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // Define a new VPC // ... // Define an RDS database cluster // ... // Get the port token (this is a token encoded as a number) Number portToken = dbCluster.getClusterEndpoint().getPort(); // ... // Convert the encoded number to an encoded string for use in the connection string String portAsString = Tokenization.stringifyNumber(portToken); // Example connection string with the port token as a string String connectionString = "jdbc:mysql://mydb.cluster.amazonaws.com:" + portAsString + "/mydatabase"; // Use the connection string as an environment variable in a Lambda function Function myFunction = Function.Builder.create(this, "MyLambdaFunction") // ... .environment(Map.of( "DATABASE_CONNECTION_STRING", connectionString // Using the port token as part of the string )) .build(); // Output the value of our connection string at synthesis System.out.println("connectionString: " + connectionString); // Output the connection string CfnOutput.Builder.create(this, "ConnectionString") .value(connectionString) .build(); } }
C#
// ... namespace CdkDemoApp { public class CdkDemoAppStack : Stack { internal CdkDemoAppStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // Define a new VPC // ... // Define an RDS database cluster // ... // Get the port token (this is a token encoded as a number) var portToken = dbCluster.ClusterEndpoint.Port; // ... // Convert the encoded number to an encoded string for use in the connection string var portAsString = Tokenization.StringifyNumber(portToken); // Example connection string with the port token as a string var connectionString = $"jdbc:mysql://mydb.cluster.amazonaws.com:{portAsString}/mydatabase"; // Use the connection string as an environment variable in a Lambda function var myFunction = new Function(this, "MyLambdaFunction", new FunctionProps { // ... Environment = new Dictionary<string, string> { { "DATABASE_CONNECTION_STRING", connectionString } // Using the port token as part of the string } }); // Output the value of our connection string at synthesis Console.WriteLine($"connectionString: {connectionString}"); // Output the connection string new CfnOutput(this, "ConnectionString", new CfnOutputProps { Value = connectionString }); } } }
Go
// ... func NewCdkDemoAppStack(scope constructs.Construct, id string, props *CdkDemoAppStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) // Define a new VPC // ... // Define an RDS database cluster // ... // Get the port token (this is a token encoded as a number) portToken := dbCluster.ClusterEndpoint().Port() // ... // Convert the encoded number to an encoded string for use in the connection string portAsString := awscdk.Tokenization_StringifyNumber(portToken) // Example connection string with the port token as a string connectionString := fmt.Sprintf("jdbc:mysql://mydb.cluster.amazonaws.com:%s/mydatabase", portAsString) // Use the connection string as an environment variable in a Lambda function myFunction := awslambda.NewFunction(stack, jsii.String("MyLambdaFunction"), &awslambda.FunctionProps{ // ... Environment: &map[string]*string{ "DATABASE_CONNECTION_STRING": jsii.String(connectionString), // Using the port token as part of the string }, }) // Output the value of our connection string at synthesis fmt.Println("connectionString: ", connectionString) // Output the connection string awscdk.NewCfnOutput(stack, jsii.String("ConnectionString"), &awscdk.CfnOutputProps{ Value: jsii.String(connectionString), }) fmt.Println(myFunction) return stack } // ...
import { Stack, Duration, Tokenization, CfnOutput, StackProps } from 'aws-cdk-lib'; // ... export class CdkDemoAppStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); // Define a new VPC // ... // Define an RDS database cluster // ... // Get the port token (this is a token encoded as a number) const portToken = dbCluster.clusterEndpoint.port; // ... // Convert the encoded number to an encoded string for use in the connection string const portAsString = Tokenization.stringifyNumber(portToken); // Example connection string with the port token as a string const connectionString = `jdbc:mysql://mydb.cluster.amazonaws.com:${portAsString}/mydatabase`; // Use the connection string as an environment variable in a Lambda function const myFunction = new lambda.Function(this, 'MyLambdaFunction', { // ... environment: { DATABASE_CONNECTION_STRING: connectionString, // Using the port token as part of the string }, }); // Output the value of our connection string at synthesis console.log("connectionString: " + connectionString); // Output the connection string new CfnOutput(this, 'ConnectionString', { value: connectionString, }); } }

cdk synth를 실행하면 연결 문자열의 값이 더 깨끗하고 명확한 형식으로 표시됩니다.

$ cdk synth --quiet connectionString: jdbc:mysql://mydb.cluster.amazonaws.com:${Token[TOKEN.242]}/mydatabase

지연 값

AWS CloudFormation 토큰은 파라미터와 같은 배포 시간 값을 나타내는 것 외에도 합성 시간 지연 값을 나타내는 데도 일반적으로 사용됩니다. 이는 값이 생성되는 시점에 결정되지 않고 합성이 완료되기 전에 최종 값이 결정되는 값입니다. 토큰을 사용하여 리터럴 문자열이나 숫자 값을 다른 구문에 전달하는데, 합성 시점의 실제 값은 아직 발생하지 않은 계산에 따라 달라질 수 있습니다.

Lazy.stringLazy.number와 같은 Lazy 클래스의 정적 메서드를 사용하여 합성 시간 지연 값을 나타내는 토큰을 작성할 수 있습니다. 이러한 메서드는 produce 속성이 컨텍스트 인수를 수락하고 직접 호출 시 최종 값을 반환하는 함수인 객체를 수락합니다.

다음 예에서는 용량이 생성된 후 결정되는 Auto Scaling 그룹을 생성합니다.

TypeScript
let actualValue: number; new AutoScalingGroup(this, 'Group', { desiredCapacity: Lazy.numberValue({ produce(context) { return actualValue; } }) }); // At some later point actualValue = 10;
JavaScript
let actualValue; new AutoScalingGroup(this, 'Group', { desiredCapacity: Lazy.numberValue({ produce(context) { return (actualValue); } }) }); // At some later point actualValue = 10;
Python
class Producer: def __init__(self, func): self.produce = func actual_value = None AutoScalingGroup(self, "Group", desired_capacity=Lazy.number_value(Producer(lambda context: actual_value)) ) # At some later point actual_value = 10
Java
double actualValue = 0; class ProduceActualValue implements INumberProducer { @Override public Number produce(IResolveContext context) { return actualValue; } } AutoScalingGroup.Builder.create(this, "Group") .desiredCapacity(Lazy.numberValue(new ProduceActualValue())).build(); // At some later point actualValue = 10;
C#
public class NumberProducer : INumberProducer { Func<Double> function; public NumberProducer(Func<Double> function) { this.function = function; } public Double Produce(IResolveContext context) { return function(); } } double actualValue = 0; new AutoScalingGroup(this, "Group", new AutoScalingGroupProps { DesiredCapacity = Lazy.NumberValue(new NumberProducer(() => actualValue)) }); // At some later point actualValue = 10;
let actualValue: number; new AutoScalingGroup(this, 'Group', { desiredCapacity: Lazy.numberValue({ produce(context) { return actualValue; } }) }); // At some later point actualValue = 10;

JSON으로 변환

임의 데이터의 JSON 문자열을 생성하려고 하는데 데이터에 토큰이 포함되어 있는지 모를 수도 있습니다. 토큰이 포함되어 있는지 여부에 관계없이 모든 데이터 구문을 적절하게 JSON 인코딩하려면 다음 예와 같이 메서드 stack.toJsonString을 사용합니다.

TypeScript
const stack = Stack.of(this); const str = stack.toJsonString({ value: bucket.bucketName });
JavaScript
const stack = Stack.of(this); const str = stack.toJsonString({ value: bucket.bucketName });
Python
stack = Stack.of(self) string = stack.to_json_string(dict(value=bucket.bucket_name))
Java
Stack stack = Stack.of(this); String stringVal = stack.toJsonString(java.util.Map.of( // Map.of requires Java 9+ put("value", bucket.getBucketName())));
C#
var stack = Stack.Of(this); var stringVal = stack.ToJsonString(new Dictionary<string, string> { ["value"] = bucket.BucketName });
const stack = Stack.of(this); const str = stack.toJsonString({ value: bucket.bucketName });
프라이버시사이트 이용 약관쿠키 기본 설정
© 2025, Amazon Web Services, Inc. 또는 계열사. All rights reserved.