サーバーレス Hello World アプリケーションを作成する - AWS Cloud Development Kit (AWS CDK) v2

これは AWS CDK v2 デベロッパーガイドです。古い CDK v1 は 2022 年 6 月 1 日にメンテナンスを開始し、2023 年 6 月 1 日にサポートを終了しました。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

サーバーレス Hello World アプリケーションを作成する

このチュートリアルでは、 を使用して、以下で構成される基本的な API バックエンドを実装するシンプルなサーバーレスHello Worldアプリケーション AWS Cloud Development Kit (AWS CDK) を作成します。

  • Amazon API Gateway REST API – HTTP GETリクエストを通じて関数を呼び出すために使用される HTTP エンドポイントを提供します。

  • AWS Lambda 関数 – HTTPエンドポイントで呼び出されたときにHello World!メッセージを返す関数。

  • 統合とアクセス許可 – リソースが相互にやり取りし、Amazon へのログの書き込みなどのアクションを実行するための設定の詳細とアクセス許可 CloudWatch。

以下の図は、このアプリケーションのコンポーネントを示しています。

API Gateway エンドポイントに GET リクエストを送信するときに呼び出される Lambda 関数の図。

このチュートリアルでは、次のステップでアプリケーションを作成して操作します。

  1. AWS CDK プロジェクトを作成します。

  2. コンストラクトライブラリの L2 コンストラクトを使用して、Lambda 関数と API Gateway REST API AWS を定義します。

  3. アプリケーションを にデプロイします AWS クラウド。

  4. でアプリケーションとやり取りします AWS クラウド。

  5. AWS クラウドからサンプルアプリケーションを削除します。

前提条件

このチュートリアルをスタートする前に、以下を完了してください。

  • AWS アカウント を作成し、 AWS Command Line Interface (AWS CLI) をインストールして設定します。

  • Node.js と をインストールしますnpm。

  • を使用して CDK Toolkit をグローバルにインストールしますnpm install -g aws-cdk

詳細については、「の開始方法 AWS CDK」を参照してください。

また、以下の基本的な理解もお勧めします。

ステップ 1: CDK プロジェクトを作成する

このステップでは、 cdk init コマンドを使用して新しい CDK AWS CDK CLIプロジェクトを作成します。

CDK プロジェクトを作成するには
  1. 選択した開始ディレクトリから、マシンcdk-hello-worldに という名前のプロジェクトディレクトリを作成して移動します。

    $ mkdir cdk-hello-world && cd cdk-hello-world
  2. cdk init コマンドを使用して、任意のプログラミング言語で新しいプロジェクトを作成します。

    TypeScript
    $ cdk init --language typescript

    AWS CDK ライブラリをインストールします。

    $ npm install aws-cdk-lib constructs
    JavaScript
    $ cdk init --language javascript

    AWS CDK ライブラリをインストールします。

    $ npm install aws-cdk-lib constructs
    Python
    $ cdk init --language python

    仮想環境をアクティブ化します。

    $ source .venv/bin/activate # On Windows, run '.\venv\Scripts\activate' instead

    AWS CDK ライブラリとプロジェクトの依存関係をインストールします。

    (.venv)$ python3 -m pip install -r requirements.txt
    Java
    $ cdk init --language java

    AWS CDK ライブラリとプロジェクトの依存関係をインストールします。

    $ mvn package
    C#
    $ cdk init --language csharp

    AWS CDK ライブラリとプロジェクトの依存関係をインストールします。

    $ dotnet restore src
    Go
    $ cdk init --language go

    プロジェクトの依存関係をインストールします。

    $ go get github.com/aws/aws-cdk-go/awscdk/v2 $ go get github.com/aws/aws-cdk-go/awscdk/v2/awslambda $ go get github.com/aws/aws-cdk-go/awscdk/v2/awsapigateway $ go mod tidy

    CDK は、次の構造でプロジェクトCLIを作成します。

    TypeScript
    cdk-hello-world ├── .git ├── .gitignore ├── .npmignore ├── README.md ├── bin │ └── cdk-hello-world.ts ├── cdk.json ├── jest.config.js ├── lib │ └── cdk-hello-world-stack.ts ├── node_modules ├── package-lock.json ├── package.json ├── test │ └── cdk-hello-world.test.ts └── tsconfig.json
    JavaScript
    cdk-hello-world ├── .git ├── .gitignore ├── .npmignore ├── README.md ├── bin │ └── cdk-hello-world.js ├── cdk.json ├── jest.config.js ├── lib │ └── cdk-hello-world-stack.js ├── node_modules ├── package-lock.json ├── package.json └── test └── cdk-hello-world.test.js
    Python
    cdk-hello-world ├── .git ├── .gitignore ├── .venv ├── README.md ├── app.py ├── cdk.json ├── cdk_hello_world │ ├── __init__.py │ └── cdk_hello_world_stack.py ├── requirements-dev.txt ├── requirements.txt ├── source.bat └── tests
    Java
    cdk-hello-world ├── .git ├── .gitignore ├── README.md ├── cdk.json ├── pom.xml ├── src │ ├── main │ │ └── java │ │ └── com │ │ └── myorg │ │ ├── CdkHelloWorldApp.java │ │ └── CdkHelloWorldStack.java └── target
    C#
    cdk-hello-world ├── .git ├── .gitignore ├── README.md ├── cdk.json └── src ├── CdkHelloWorld │ ├── CdkHelloWorld.csproj │ ├── CdkHelloWorldStack.cs │ ├── GlobalSuppressions.cs │ └── Program.cs └── CdkHelloWorld.sln
    Go
    cdk-hello-world ├── .git ├── .gitignore ├── README.md ├── cdk-hello-world.go ├── cdk-hello-world_test.go ├── cdk.json ├── go.mod └── go.sum

CDK は、単一のスタックを含む CDK アプリCLIを自動的に作成します。CDK アプリケーションインスタンスは App クラスから作成されます。CDK アプリケーションファイルの一部を次に示します。

TypeScript

にありますbin/cdk-hello-world.ts

#!/usr/bin/env node import 'source-map-support/register'; import * as cdk from 'aws-cdk-lib'; import { CdkHelloWorldStack } from '../lib/cdk-hello-world-stack'; const app = new cdk.App(); new CdkHelloWorldStack(app, 'CdkHelloWorldStack', { });
JavaScript

にありますbin/cdk-hello-world.js

#!/usr/bin/env node const cdk = require('aws-cdk-lib'); const { CdkHelloWorldStack } = require('../lib/cdk-hello-world-stack'); const app = new cdk.App(); new CdkHelloWorldStack(app, 'CdkHelloWorldStack', { });
Python

にありますapp.py

#!/usr/bin/env python3 import os import aws_cdk as cdk from cdk_hello_world.cdk_hello_world_stack import CdkHelloWorldStack app = cdk.App() CdkHelloWorldStack(app, "CdkHelloWorldStack",) app.synth()
Java

にありますsrc/main/java/.../CdkHelloWorldApp.java

package com.myorg; import software.amazon.awscdk.App; import software.amazon.awscdk.Environment; import software.amazon.awscdk.StackProps; import java.util.Arrays; public class JavaApp { public static void main(final String[] args) { App app = new App(); new JavaStack(app, "JavaStack", StackProps.builder() .build()); app.synth(); } }
C#

にありますsrc/CdkHelloWorld/Program.cs

using Amazon.CDK; using System; using System.Collections.Generic; using System.Linq; namespace CdkHelloWorld { sealed class Program { public static void Main(string[] args) { var app = new App(); new CdkHelloWorldStack(app, "CdkHelloWorldStack", new StackProps { }); app.Synth(); } } }
Go

にありますcdk-hello-world.go

package main import ( "github.com/aws/aws-cdk-go/awscdk/v2" "github.com/aws/constructs-go/constructs/v10" "github.com/aws/jsii-runtime-go" ) // ... func main() { defer jsii.Close() app := awscdk.NewApp(nil) NewCdkHelloWorldStack(app, "CdkHelloWorldStack", &CdkHelloWorldStackProps{ awscdk.StackProps{ Env: env(), }, }) app.Synth(nil) } func env() *awscdk.Environment { return nil }

ステップ 2: Lambda 関数を作成する

CDK プロジェクト内で、新しいhello.jsファイルを含むlambdaディレクトリを作成します。以下に例を示します。

TypeScript

プロジェクトのルートから、以下を実行します。

$ mkdir lambda && cd lambda $ touch hello.js

これで、CDK プロジェクトに以下が追加されます。

cdk-hello-world └── lambda └── hello.js
JavaScript

プロジェクトのルートから、以下を実行します。

$ mkdir lambda && cd lambda $ touch hello.js

これで、CDK プロジェクトに以下が追加されます。

cdk-hello-world └── lambda └── hello.js
Python

プロジェクトのルートから、以下を実行します。

$ mkdir lambda && cd lambda $ touch hello.js

これで、CDK プロジェクトに以下が追加されます。

cdk-hello-world └── lambda └── hello.js
Java

プロジェクトのルートから、以下を実行します。

$ mkdir -p src/main/resources/lambda $ cd src/main/resources/lambda $ touch hello.js

これで、CDK プロジェクトに以下が追加されます。

cdk-hello-world └── src └── main └──resources └──lambda └──hello.js
C#

プロジェクトのルートから、以下を実行します。

$ mkdir lambda && cd lambda $ touch hello.js

これで、CDK プロジェクトに以下が追加されます。

cdk-hello-world └── lambda └── hello.js
Go

プロジェクトのルートから、以下を実行します。

$ mkdir lambda && cd lambda $ touch hello.js

これで、CDK プロジェクトに以下が追加されます。

cdk-hello-world └── lambda └── hello.js
注記

このチュートリアルを簡単にするために、すべての CDK プログラミング言語に JavaScript Lambda 関数を使用します。

新しく作成されたファイルに以下を追加して、Lambda 関数を定義します。

exports.handler = async (event) => { return { statusCode: 200, headers: { "Content-Type": "text/plain" }, body: JSON.stringify({ message: "Hello, World!" }), }; };

ステップ 3: コンストラクトを定義する

このステップでは、 AWS CDK L2 コンストラクトを使用して Lambda リソースと API Gateway リソースを定義します。

CDK スタックを定義するプロジェクトファイルを開きます。このファイルを変更してコンストラクトを定義します。開始スタックファイルの例を次に示します。

TypeScript

にありますlib/cdk-hello-world-stack.ts

import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; export class CdkHelloWorldStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Your constructs will go here } }
JavaScript

にありますlib/cdk-hello-world-stack.js

const { Stack, Duration } = require('aws-cdk-lib'); const lambda = require('aws-cdk-lib/aws-lambda'); const apigateway = require('aws-cdk-lib/aws-apigateway'); class CdkHelloWorldStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // Your constructs will go here } } module.exports = { CdkHelloWorldStack }
Python

にありますcdk_hello_world/cdk_hello_world_stack.py

from aws_cdk import Stack from constructs import Construct class CdkHelloWorldStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) // Your constructs will go here
Java

にありますsrc/main/java/.../CdkHelloWorldStack.java

package com.myorg; import software.constructs.Construct; import software.amazon.awscdk.Stack; import software.amazon.awscdk.StackProps; public class CdkHelloWorldStack extends Stack { public CdkHelloWorldStack(final Construct scope, final String id) { this(scope, id, null); } public CdkHelloWorldStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // Your constructs will go here } }
C#

にありますsrc/CdkHelloWorld/CdkHelloWorldStack.cs

using Amazon.CDK; using Constructs; namespace CdkHelloWorld { public class CdkHelloWorldStack : Stack { internal CdkHelloWorldStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // Your constructs will go here } } }
Go

の場所cdk-hello-world.go

package main import ( "github.com/aws/aws-cdk-go/awscdk/v2" "github.com/aws/constructs-go/constructs/v10" "github.com/aws/jsii-runtime-go" ) type CdkHelloWorldStackProps struct { awscdk.StackProps } func NewCdkHelloWorldStack(scope constructs.Construct, id string, props *CdkHelloWorldStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) // Your constructs will go here return stack } func main() { // ... } func env() *awscdk.Environment { return nil }

このファイルでは、 AWS CDK は以下を実行します。

  • CDK スタックインスタンスは Stack クラスからインスタンス化されます。

  • Constructs ベースクラスはインポートされ、スタックインスタンスのスコープまたは親として提供されます。

Lambda 関数リソースを定義する

Lambda 関数リソースを定義するには、コンストラクトライブラリから aws-lambda L2 AWS コンストラクトをインポートして使用します。

スタックファイルを次のように変更します。

TypeScript
import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; // Import Lambda L2 construct import * as lambda from 'aws-cdk-lib/aws-lambda'; export class CdkHelloWorldStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Define the Lambda function resource const helloWorldFunction = new lambda.Function(this, 'HelloWorldFunction', { runtime: lambda.Runtime.NODEJS_20_X, // Choose any supported Node.js runtime code: lambda.Code.fromAsset('lambda'), // Points to the lambda directory handler: 'hello.handler', // Points to the 'hello' file in the lambda directory }); } }
JavaScript
const { Stack, Duration } = require('aws-cdk-lib'); // Import Lambda L2 construct const lambda = require('aws-cdk-lib/aws-lambda'); class CdkHelloWorldStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // Define the Lambda function resource const helloWorldFunction = new lambda.Function(this, 'HelloWorldFunction', { runtime: lambda.Runtime.NODEJS_20_X, // Choose any supported Node.js runtime code: lambda.Code.fromAsset('lambda'), // Points to the lambda directory handler: 'hello.handler', // Points to the 'hello' file in the lambda directory }); } } module.exports = { CdkHelloWorldStack }
Python
from aws_cdk import ( Stack, # Import Lambda L2 construct aws_lambda as _lambda, ) # ... class CdkHelloWorldStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # Define the Lambda function resource hello_world_function = _lambda.Function( self, "HelloWorldFunction", runtime = _lambda.Runtime.NODEJS_20_X, # Choose any supported Node.js runtime code = _lambda.Code.from_asset("lambda"), # Points to the lambda directory handler = "hello.handler", # Points to the 'hello' file in the lambda directory )
注記

lambda は の組み込み識別子_lambdaであるため、aws_lambdaモジュールは としてインポートされますPython。

Java
// ... // Import Lambda L2 construct import software.amazon.awscdk.services.lambda.Code; import software.amazon.awscdk.services.lambda.Function; import software.amazon.awscdk.services.lambda.Runtime; public class CdkHelloWorldStack extends Stack { public CdkHelloWorldStack(final Construct scope, final String id) { this(scope, id, null); } public CdkHelloWorldStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // Define the Lambda function resource Function helloWorldFunction = Function.Builder.create(this, "HelloWorldFunction") .runtime(Runtime.NODEJS_20_X) // Choose any supported Node.js runtime .code(Code.fromAsset("src/main/resources/lambda")) // Points to the lambda directory .handler("hello.handler") // Points to the 'hello' file in the lambda directory .build(); } }
C#
// ... // Import Lambda L2 construct using Amazon.CDK.AWS.Lambda; namespace CdkHelloWorld { public class CdkHelloWorldStack : Stack { internal CdkHelloWorldStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // Define the Lambda function resource var helloWorldFunction = new Function(this, "HelloWorldFunction", new FunctionProps { Runtime = Runtime.NODEJS_20_X, // Choose any supported Node.js runtime Code = Code.FromAsset("lambda"), // Points to the lambda directory Handler = "hello.handler" // Points to the 'hello' file in the lambda directory }); } } }
Go
package main import ( // ... // Import Lambda L2 construct "github.com/aws/aws-cdk-go/awscdk/v2/awslambda" // Import S3 assets construct "github.com/aws/aws-cdk-go/awscdk/v2/awss3assets" // ... ) // ... func NewCdkHelloWorldStack(scope constructs.Construct, id string, props *CdkHelloWorldStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) // Define the Lambda function resource helloWorldFunction := awslambda.NewFunction(stack, jsii.String("HelloWorldFunction"), &awslambda.FunctionProps{ Runtime: awslambda.Runtime_NODEJS_20_X(), // Choose any supported Node.js runtime Code: awslambda.Code_FromAsset(jsii.String("lambda"), &awss3assets.AssetOptions{}), // Points to the lambda directory Handler: jsii.String("hello.handler"), // Points to the 'hello' file in the lambda directory }) return stack } // ...

ここでは、Lambda 関数リソースを作成し、次のプロパティを定義します。

  • runtime – 関数が実行される環境。ここでは、Node.jsバージョン を使用します20.x。

  • code – ローカルマシン上の関数コードへのパス。

  • handler – 関数コードを含む特定のファイルの名前。

API Gateway REST APIリソースを定義する

API Gateway REST APIリソースを定義するには、コンストラクトライブラリから aws-apigateway L2 AWS コンストラクトをインポートして使用します。

スタックファイルを次のように変更します。

TypeScript
// ... //Import API Gateway L2 construct import * as apigateway from 'aws-cdk-lib/aws-apigateway'; export class CdkHelloWorldStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // ... // Define the API Gateway resource const api = new apigateway.LambdaRestApi(this, 'HelloWorldApi', { handler: helloWorldFunction, proxy: false, }); // Define the '/hello' resource with a GET method const helloResource = api.root.addResource('hello'); helloResource.addMethod('GET'); } }
JavaScript
// ... // Import API Gateway L2 construct const apigateway = require('aws-cdk-lib/aws-apigateway'); class CdkHelloWorldStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // ... // Define the API Gateway resource const api = new apigateway.LambdaRestApi(this, 'HelloWorldApi', { handler: helloWorldFunction, proxy: false, }); // Define the '/hello' resource with a GET method const helloResource = api.root.addResource('hello'); helloResource.addMethod('GET'); }; }; // ...
Python
from aws_cdk import ( # ... # Import API Gateway L2 construct aws_apigateway as apigateway, ) from constructs import Construct class CdkHelloWorldStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) # ... # Define the API Gateway resource api = apigateway.LambdaRestApi( self, "HelloWorldApi", handler = hello_world_function, proxy = False, ) # Define the '/hello' resource with a GET method hello_resource = api.root.add_resource("hello") hello_resource.add_method("GET")
Java
// ... // Import API Gateway L2 construct import software.amazon.awscdk.services.apigateway.LambdaRestApi; import software.amazon.awscdk.services.apigateway.Resource; public class CdkHelloWorldStack extends Stack { public CdkHelloWorldStack(final Construct scope, final String id) { this(scope, id, null); } public CdkHelloWorldStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); // ... // Define the API Gateway resource LambdaRestApi api = LambdaRestApi.Builder.create(this, "HelloWorldApi") .handler(helloWorldFunction) .proxy(false) // Turn off default proxy integration .build(); // Define the '/hello' resource and its GET method Resource helloResource = api.getRoot().addResource("hello"); helloResource.addMethod("GET"); } }
C#
// ... // Import API Gateway L2 construct using Amazon.CDK.AWS.APIGateway; namespace CdkHelloWorld { public class CdkHelloWorldStack : Stack { internal CdkHelloWorldStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props) { // ... // Define the API Gateway resource var api = new LambdaRestApi(this, "HelloWorldApi", new LambdaRestApiProps { Handler = helloWorldFunction, Proxy = false }); // Add a '/hello' resource with a GET method var helloResource = api.Root.AddResource("hello"); helloResource.AddMethod("GET"); } } }
Go

// ... import ( // ... // Import Api Gateway L2 construct "github.com/aws/aws-cdk-go/awscdk/v2/awsapigateway" // ... ) // ... func NewCdkHelloWorldStack(scope constructs.Construct, id string, props *CdkHelloWorldStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) // Define the Lambda function resource // ... // Define the API Gateway resource api := awsapigateway.NewLambdaRestApi(stack, jsii.String("HelloWorldApi"), &awsapigateway.LambdaRestApiProps{ Handler: helloWorldFunction, Proxy: jsii.Bool(false), }) // Add a '/hello' resource with a GET method helloResource := api.Root().AddResource(jsii.String("hello"), &awsapigateway.ResourceOptions{}) helloResource.AddMethod(jsii.String("GET"), awsapigateway.NewLambdaIntegration(helloWorldFunction, &awsapigateway.LambdaIntegrationOptions{}), &awsapigateway.MethodOptions{}) return stack } // ...

ここでは、API Gateway REST APIリソースと以下を作成します。

  • REST API と Lambda 関数の統合。API が関数を呼び出せるようにします。これには、Lambda アクセス許可リソースの作成が含まれます。

  • API エンドポイントのルートに追加helloされる という名前の新しいリソースまたはパス。これにより、 をベース /helloに追加する新しいエンドポイントが作成されますURL。

  • hello リソースの GET メソッド。GET リクエストが/helloエンドポイントに送信されると、Lambda 関数が呼び出され、そのレスポンスが返されます。

ステップ 4: アプリケーションをデプロイ用に準備する

このステップでは、必要に応じて、 cdk synth コマンドを使用して基本的な検証を構築して、アプリケーションをデプロイ用に準備します AWS CDK CLI。

必要に応じて、アプリケーションを構築します。

TypeScript

プロジェクトのルートから、以下を実行します。

$ npm run build
JavaScript

構築は必要ありません。

Python

構築は必要ありません。

Java

プロジェクトのルートから、以下を実行します。

$ mvn package
C#

プロジェクトのルートから、以下を実行します。

$ dotnet build src
Go

構築は必要ありません。

cdk synth を実行して CDK コードから AWS CloudFormation テンプレートを合成します。L2 コンストラクトを使用すると、Lambda 関数 と 間のやり取りを容易に AWS CloudFormation するために が必要とする設定の詳細の多くはREST API、 によってプロビジョニングされます AWS CDK。

プロジェクトのルートから、以下を実行します。

$ cdk synth
注記

次のようなエラーが表示された場合は、 cdk-hello-world ディレクトリにあることを確認し、もう一度試してください。

--app is required either in command-line, in cdk.json or in ~/.cdk.json

成功すると AWS CDK CLI、 はコマンドプロンプトで AWS CloudFormation テンプレートを YAML 形式で出力します。フォーマットJSONされたテンプレートも cdk.out ディレクトリに保存されます。

テンプレートの出力例を次に示します AWS CloudFormation 。

Resources: HelloWorldFunctionServiceRoleunique-identifier: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Action: sts:AssumeRole Effect: Allow Principal: Service: lambda.amazonaws.com Version: "2012-10-17" ManagedPolicyArns: - Fn::Join: - "" - - "arn:" - Ref: AWS::Partition - :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldFunction/ServiceRole/Resource HelloWorldFunctionunique-identifier: Type: AWS::Lambda::Function Properties: Code: S3Bucket: Fn::Sub: cdk-unique-identifier-assets-${AWS::AccountId}-${AWS::Region} S3Key: unique-identifier.zip Handler: hello.handler Role: Fn::GetAtt: - HelloWorldFunctionServiceRoleunique-identifier - Arn Runtime: nodejs20.x DependsOn: - HelloWorldFunctionServiceRoleunique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldFunction/Resource aws:asset:path: asset.unique-identifier aws:asset:is-bundled: false aws:asset:property: Code HelloWorldApiunique-identifier: Type: AWS::ApiGateway::RestApi Properties: Name: HelloWorldApi Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Resource HelloWorldApiDeploymentunique-identifier: Type: AWS::ApiGateway::Deployment Properties: Description: Automatically created by the RestApi construct RestApiId: Ref: HelloWorldApiunique-identifier DependsOn: - HelloWorldApihelloGETunique-identifier - HelloWorldApihellounique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Deployment/Resource HelloWorldApiDeploymentStageprod012345ABC: Type: AWS::ApiGateway::Stage Properties: DeploymentId: Ref: HelloWorldApiDeploymentunique-identifier RestApiId: Ref: HelloWorldApiunique-identifier StageName: prod Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/DeploymentStage.prod/Resource HelloWorldApihellounique-identifier: Type: AWS::ApiGateway::Resource Properties: ParentId: Fn::GetAtt: - HelloWorldApiunique-identifier - RootResourceId PathPart: hello RestApiId: Ref: HelloWorldApiunique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Default/hello/Resource HelloWorldApihelloGETApiPermissionCdkHelloWorldStackHelloWorldApiunique-identifier: Type: AWS::Lambda::Permission Properties: Action: lambda:InvokeFunction FunctionName: Fn::GetAtt: - HelloWorldFunctionunique-identifier - Arn Principal: apigateway.amazonaws.com SourceArn: Fn::Join: - "" - - "arn:" - Ref: AWS::Partition - ":execute-api:" - Ref: AWS::Region - ":" - Ref: AWS::AccountId - ":" - Ref: HelloWorldApi9E278160 - / - Ref: HelloWorldApiDeploymentStageprodunique-identifier - /GET/hello Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Default/hello/GET/ApiPermission.CdkHelloWorldStackHelloWorldApiunique-identifier.GET..hello HelloWorldApihelloGETApiPermissionTestCdkHelloWorldStackHelloWorldApiunique-identifier: Type: AWS::Lambda::Permission Properties: Action: lambda:InvokeFunction FunctionName: Fn::GetAtt: - HelloWorldFunctionunique-identifier - Arn Principal: apigateway.amazonaws.com SourceArn: Fn::Join: - "" - - "arn:" - Ref: AWS::Partition - ":execute-api:" - Ref: AWS::Region - ":" - Ref: AWS::AccountId - ":" - Ref: HelloWorldApiunique-identifier - /test-invoke-stage/GET/hello Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Default/hello/GET/ApiPermission.Test.CdkHelloWorldStackHelloWorldApiunique-identifier.GET..hello HelloWorldApihelloGETunique-identifier: Type: AWS::ApiGateway::Method Properties: AuthorizationType: NONE HttpMethod: GET Integration: IntegrationHttpMethod: POST Type: AWS_PROXY Uri: Fn::Join: - "" - - "arn:" - Ref: AWS::Partition - ":apigateway:" - Ref: AWS::Region - :lambda:path/2015-03-31/functions/ - Fn::GetAtt: - HelloWorldFunctionunique-identifier - Arn - /invocations ResourceId: Ref: HelloWorldApihellounique-identifier RestApiId: Ref: HelloWorldApiunique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/HelloWorldApi/Default/hello/GET/Resource CDKMetadata: Type: AWS::CDK::Metadata Properties: Analytics: v2:deflate64:unique-identifier Metadata: aws:cdk:path: CdkHelloWorldStack/CDKMetadata/Default Condition: CDKMetadataAvailable Outputs: HelloWorldApiEndpointunique-identifier: Value: Fn::Join: - "" - - https:// - Ref: HelloWorldApiunique-identifier - .execute-api. - Ref: AWS::Region - "." - Ref: AWS::URLSuffix - / - Ref: HelloWorldApiDeploymentStageprodunique-identifier - / Conditions: CDKMetadataAvailable: Fn::Or: - Fn::Or: - Fn::Equals: - Ref: AWS::Region - af-south-1 - Fn::Equals: - Ref: AWS::Region - ap-east-1 - Fn::Equals: - Ref: AWS::Region - ap-northeast-1 - Fn::Equals: - Ref: AWS::Region - ap-northeast-2 - Fn::Equals: - Ref: AWS::Region - ap-south-1 - Fn::Equals: - Ref: AWS::Region - ap-southeast-1 - Fn::Equals: - Ref: AWS::Region - ap-southeast-2 - Fn::Equals: - Ref: AWS::Region - ca-central-1 - Fn::Equals: - Ref: AWS::Region - cn-north-1 - Fn::Equals: - Ref: AWS::Region - cn-northwest-1 - Fn::Or: - Fn::Equals: - Ref: AWS::Region - eu-central-1 - Fn::Equals: - Ref: AWS::Region - eu-north-1 - Fn::Equals: - Ref: AWS::Region - eu-south-1 - Fn::Equals: - Ref: AWS::Region - eu-west-1 - Fn::Equals: - Ref: AWS::Region - eu-west-2 - Fn::Equals: - Ref: AWS::Region - eu-west-3 - Fn::Equals: - Ref: AWS::Region - il-central-1 - Fn::Equals: - Ref: AWS::Region - me-central-1 - Fn::Equals: - Ref: AWS::Region - me-south-1 - Fn::Equals: - Ref: AWS::Region - sa-east-1 - Fn::Or: - Fn::Equals: - Ref: AWS::Region - us-east-1 - Fn::Equals: - Ref: AWS::Region - us-east-2 - Fn::Equals: - Ref: AWS::Region - us-west-1 - Fn::Equals: - Ref: AWS::Region - us-west-2 Parameters: BootstrapVersion: Type: AWS::SSM::Parameter::Value<String> Default: /cdk-bootstrap/hnb659fds/version Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip] Rules: CheckBootstrapVersion: Assertions: - Assert: Fn::Not: - Fn::Contains: - - "1" - "2" - "3" - "4" - "5" - Ref: BootstrapVersion AssertDescription: CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.

L2 コンストラクトを使用して、リソースを設定するためのいくつかのプロパティを定義し、ヘルパーメソッドを使用してそれらを統合します。は、アプリケーションのプロビジョニングに必要な AWS CloudFormation リソースとプロパティの大部分 AWS CDK を設定します。

ステップ 5: アプリケーションをデプロイする

このステップでは、 cdk deploy コマンドを使用して AWS CDK CLIアプリケーションをデプロイします。 AWS CDK は AWS CloudFormation サービスと連携してリソースをプロビジョニングします。

重要

デプロイする前に、 AWS 環境の 1 回限りのブートストラップを実行する必要があります。手順については、「環境のブートストラップ」を参照してください。

プロジェクトのルートから、以下を実行します。プロンプトが表示されたら、変更を確認します。

$ cdk deploy ✨ Synthesis time: 2.44s ... Do you wish to deploy these changes (y/n)? y

デプロイが完了すると、 はエンドポイント URL を AWS CDK CLI出力します。この URL を次のステップにコピーします。以下に例を示します。

... ✅ HelloWorldStack ✨ Deployment time: 45.37s Outputs: HelloWorldStack.HelloWorldApiEndpointunique-identifier = https://<api-id>.execute-api.<region>.amazonaws.com/prod/ Stack ARN: arn:aws:cloudformation:region:account-id:stack/HelloWorldStack/unique-identifier ...

ステップ 6: アプリケーションを操作する

このステップでは、API エンドポイントへの GET リクエストを開始し、Lambda 関数のレスポンスを受け取ります。

前のステップのエンドポイント URL を見つけて、/helloパスを追加します。次に、ブラウザまたはコマンドプロンプトを使用して、エンドポイントに GET リクエストを送信します。以下に例を示します。

$ curl https://<api-id>.execute-api.<region>.amazonaws.com/prod/hello {"message":"Hello World!"}%

これで、 を使用してアプリケーションの作成、デプロイ、操作が正常に完了しました AWS CDK。

ステップ 7: アプリケーションを削除する

このステップでは、 を使用して AWS CDK CLIからアプリケーションを削除します AWS クラウド。

アプリケーションを削除するには、 を実行しますcdk destroy。プロンプトが表示されたら、アプリケーションを削除するリクエストを確認します。

$ cdk destroy Are you sure you want to delete: CdkHelloWorldStack (y/n)? y CdkHelloWorldStack: destroying... [1/1] ... ✅ CdkHelloWorldStack: destroyed

トラブルシューティング

エラー: {「メッセージ」:「内部サーバーエラー」}%

デプロイされた Lambda 関数を呼び出すと、このエラーが表示されます。このエラーは、複数の理由で発生する可能性があります。

さらにトラブルシューティングを行うには

を使用して AWS CLI Lambda 関数を呼び出します。

  1. スタックファイルを変更して、デプロイされた Lambda 関数名の出力値をキャプチャします。以下に例を示します。

    ... class CdkHelloWorldStack extends Stack { constructor(scope, id, props) { super(scope, id, props); // Define the Lambda function resource // ... new CfnOutput(this, 'HelloWorldFunctionName', { value: helloWorldFunction.functionName, description: 'JavaScript Lambda function' }); // Define the API Gateway resource // ...
  2. アプリケーションをもう一度デプロイします。 AWS CDK CLI は、デプロイされた Lambda 関数名の値を出力します。

    $ cdk deploy ✨ Synthesis time: 0.29s ... ✅ CdkHelloWorldStack ✨ Deployment time: 20.36s Outputs: ... CdkHelloWorldStack.HelloWorldFunctionName = CdkHelloWorldStack-HelloWorldFunctionunique-identifier ...
  3. を使用して AWS CLI で Lambda 関数を呼び出し AWS クラウド 、レスポンスをテキストファイルに出力します。

    $ aws lambda invoke --function-name CdkHelloWorldStack-HelloWorldFunctionunique-identifier output.txt
  4. 結果output.txtを確認します。

考えられる原因: API Gateway リソースがスタックファイルに誤って定義されています。

output.txt が Lambda 関数のレスポンスに成功した場合、API Gateway REST API の定義方法に問題がある可能性があります。は、エンドポイントではなく Lambda を直接 AWS CLI 呼び出します。コードをチェックして、このチュートリアルと一致することを確認してください。次に、再度デプロイします。

考えられる原因: Lambda リソースがスタックファイルに誤って定義されています。

がエラーをoutput.txt返す場合、問題は Lambda 関数の定義方法にある可能性があります。コードをチェックして、このチュートリアルと一致することを確認してください。次に、再度デプロイします。