As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.
Passo a passo - Parte 2
O AWS Solutions Constructs é compatível com versões ≥ 1.46.0 do AWS.
Este tutorial mostra como modificar o aplicativo “Hello Constructs” criado emparte 1. Nossa modificação adicionará um contador de visitas ao site usando o padrão AWS Lambda ao DynamoDB de Constructs de soluções da AWS. Modificar o aplicativo Hello Constructs resultará na seguinte solução:
Código do Lambda do contador
Vamos começar escrevendo o código para a função Hit Counter AWS Lambda. Essa função irá:
-
incrementar um contador relacionado ao caminho da API em uma tabela do Amazon DynamoDB,
-
invocar a função downstream Hello AWS Lambda,
-
e retorne a resposta ao usuário final.
- TypeScript
-
Adicionar um arquivo chamadolambda/hitcounter.js
com o seguinte conteúdo:
const { DynamoDB, Lambda } = require('aws-sdk');
exports.handler = async function(event) {
console.log("request:", JSON.stringify(event, undefined, 2));
// create AWS SDK clients
const dynamo = new DynamoDB();
const lambda = new Lambda();
// update dynamo entry for "path" with hits++
await dynamo.updateItem({
TableName: process.env.DDB_TABLE_NAME,
Key: { path: { S: event.path } },
UpdateExpression: 'ADD hits :incr',
ExpressionAttributeValues: { ':incr': { N: '1' } }
}).promise();
// call downstream function and capture response
const resp = await lambda.invoke({
FunctionName: process.env.DOWNSTREAM_FUNCTION_NAME,
Payload: JSON.stringify(event)
}).promise();
console.log('downstream response:', JSON.stringify(resp, undefined, 2));
// return response back to upstream caller
return JSON.parse(resp.Payload);
};
- Python
-
Adicionar um arquivo chamadolambda/hitcounter.py
com o seguinte conteúdo:
import json
import os
import boto3
ddb = boto3.resource('dynamodb')
table = ddb.Table(os.environ['DDB_TABLE_NAME'])
_lambda = boto3.client('lambda')
def handler(event, context):
print('request: {}'.format(json.dumps(event)))
table.update_item(
Key={'path': event['path']},
UpdateExpression='ADD hits :incr',
ExpressionAttributeValues={':incr': 1}
)
resp = _lambda.invoke(
FunctionName=os.environ['DOWNSTREAM_FUNCTION_NAME'],
Payload=json.dumps(event),
)
body = resp['Payload'].read()
print('downstream response: {}'.format(body))
return json.loads(body)
Instalar as novas dependências
Lembre-se de substituir a versão correta e correspondente a ser usada para construções de soluções da AWS e para o CDK da AWS noVERSION_NUMBER
campos de espaço reservado para cada comando. Isso deve ser idêntico ao número de versão usado para dependências na primeira parte deste passo a passo. Versões incompatíveis entre pacotes podem causar erros.
Como de costume, primeiro precisamos instalar as dependências necessárias para a atualização da nossa solução. Primeiro, precisamos instalar a biblioteca de construção do DynamoDB:
- TypeScript
-
npm install -s @aws-cdk/aws-dynamodb@VERSION_NUMBER
- Python
-
pip install aws_cdk.aws_dynamodb==VERSION_NUMBER
Por fim, instale os Construtos de soluções da AWSaws-lambda-dynamodb
e todas as suas dependências em nosso projeto:
- TypeScript
-
npm install -s @aws-solutions-constructs/aws-lambda-dynamodb@VERSION_NUMBER
- Python
-
pip install aws_solutions_constructs.aws_lambda_dynamodb==VERSION_NUMBER
Defina os recursos
Agora, vamos atualizar nosso código de pilha para acomodar nossa nova arquitetura.
Primeiro, vamos importar nossas novas dependências e mover a função “Olá” para fora doaws-apigateway-lambda
padrão que criamos na parte 1.
- TypeScript
-
Editar o arquivolib/hello-constructs.ts
com o seguinte:
import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as api from '@aws-cdk/aws-apigateway';
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import { ApiGatewayToLambda, ApiGatewayToLambdaProps } from '@aws-solutions-constructs/aws-apigateway-lambda';
import { LambdaToDynamoDB, LambdaToDynamoDBProps } from '@aws-solutions-constructs/aws-lambda-dynamodb';
export class HelloConstructsStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
const helloFunc = new lambda.Function(this, 'HelloHandler', {
runtime: lambda.Runtime.NODEJS_12_X,
code: lambda.Code.fromAsset('lambda'),
handler: 'hello.handler'
});
const api_lambda_props: ApiGatewayToLambdaProps = {
lambdaFunctionProps: {
code: lambda.Code.fromAsset('lambda'),
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'hello.handler'
},
apiGatewayProps: {
defaultMethodOptions: {
authorizationType: api.AuthorizationType.NONE
}
}
};
new ApiGatewayToLambda(this, 'ApiGatewayToLambda', api_lambda_props);
}
}
- Python
-
Editar o arquivohello_constructs/hello_constructs_stack.py
com o seguinte:
from aws_cdk import (
aws_lambda as _lambda,
aws_apigateway as apigw,
aws_dynamodb as ddb,
core,
)
from aws_solutions_constructs import (
aws_apigateway_lambda as apigw_lambda,
aws_lambda_dynamodb as lambda_ddb
)
class HelloConstructsStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# The code that defines your stack goes here
self._handler = _lambda.Function(
self, 'HelloHandler',
runtime=_lambda.Runtime.PYTHON_3_7,
handler='hello.handler',
code=_lambda.Code.asset('lambda'),
)
apigw_lambda.ApiGatewayToLambda(
self, 'ApiGatewayToLambda',
lambda_function_props=_lambda.FunctionProps(
runtime=_lambda.Runtime.PYTHON_3_7,
code=_lambda.Code.asset('lambda'),
handler='hello.handler',
),
api_gateway_props=apigw.RestApiProps(
default_method_options=apigw.MethodOptions(
authorization_type=apigw.AuthorizationType.NONE
)
)
)
Em seguida, vamos adicionar oaws-lambda-dynamodb
padrão para construir o serviço de contador de sucesso para nossa arquitetura atualizada.
A próxima atualização abaixo define as propriedades para oaws-lambda-dynamodb
definindo a função do AWS Lambda com o manipulador Hit Counter. Além disso, a tabela do Amazon DynamoDB é definida com um nome deHits
e uma chave de partição dopath
.
- TypeScript
-
Editar o arquivolib/hello-constructs.ts
com o seguinte:
import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as api from '@aws-cdk/aws-apigateway';
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import { ApiGatewayToLambda, ApiGatewayToLambdaProps } from '@aws-solutions-constructs/aws-apigateway-lambda';
import { LambdaToDynamoDB, LambdaToDynamoDBProps } from '@aws-solutions-constructs/aws-lambda-dynamodb';
export class HelloConstructsStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
const helloFunc = new lambda.Function(this, 'HelloHandler', {
runtime: lambda.Runtime.NODEJS_12_X,
code: lambda.Code.fromAsset('lambda'),
handler: 'hello.handler'
});
// hit counter, aws-lambda-dynamodb pattern
const lambda_ddb_props: LambdaToDynamoDBProps = {
lambdaFunctionProps: {
code: lambda.Code.asset(`lambda`),
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'hitcounter.handler',
environment: {
DOWNSTREAM_FUNCTION_NAME: helloFunc.functionName
}
},
dynamoTableProps: {
tableName: 'Hits',
partitionKey: { name: 'path', type: dynamodb.AttributeType.STRING }
}
};
const hitcounter = new LambdaToDynamoDB(this, 'LambdaToDynamoDB', lambda_ddb_props);
const api_lambda_props: ApiGatewayToLambdaProps = {
lambdaFunctionProps: {
code: lambda.Code.fromAsset('lambda'),
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'hello.handler'
},
apiGatewayProps: {
defaultMethodOptions: {
authorizationType: api.AuthorizationType.NONE
}
}
};
new ApiGatewayToLambda(this, 'ApiGatewayToLambda', api_lambda_props);
}
}
- Python
-
Editar o arquivohello_constructs/hello_constructs_stack.py
com o seguinte:
from aws_cdk import (
aws_lambda as _lambda,
aws_apigateway as apigw,
aws_dynamodb as ddb,
core,
)
from aws_solutions_constructs import (
aws_apigateway_lambda as apigw_lambda,
aws_lambda_dynamodb as lambda_ddb
)
class HelloConstructsStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# The code that defines your stack goes here
self.hello_func = _lambda.Function(
self, 'HelloHandler',
runtime=_lambda.Runtime.PYTHON_3_7,
handler='hello.handler',
code=_lambda.Code.asset('lambda'),
)
# hit counter, aws-lambda-dynamodb pattern
self.hit_counter = lambda_ddb.LambdaToDynamoDB(
self, 'LambdaToDynamoDB',
lambda_function_props=_lambda.FunctionProps(
runtime=_lambda.Runtime.PYTHON_3_7,
code=_lambda.Code.asset('lambda'),
handler='hitcounter.handler',
environment={
'DOWNSTREAM_FUNCTION_NAME': self.hello_func.function_name
}
),
dynamo_table_props=ddb.TableProps(
table_name='Hits',
partition_key={
'name': 'path',
'type': ddb.AttributeType.STRING
}
)
)
apigw_lambda.ApiGatewayToLambda(
self, 'ApiGatewayToLambda',
lambda_function_props=_lambda.FunctionProps(
runtime=_lambda.Runtime.PYTHON_3_7,
code=_lambda.Code.asset('lambda'),
handler='hello.handler',
),
api_gateway_props=apigw.RestApiProps(
default_method_options=apigw.MethodOptions(
authorization_type=apigw.AuthorizationType.NONE
)
)
)
Em seguida, precisaremos conceder a função Contador de Hits criada a partir doaws-lambda-dynamodb
padrão adicionado acima permissão para invocar nossa função Hello.
- TypeScript
-
Editar o arquivolib/hello-constructs.ts
com o seguinte:
import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as api from '@aws-cdk/aws-apigateway';
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import { ApiGatewayToLambda, ApiGatewayToLambdaProps } from '@aws-solutions-constructs/aws-apigateway-lambda';
import { LambdaToDynamoDB, LambdaToDynamoDBProps } from '@aws-solutions-constructs/aws-lambda-dynamodb';
export class HelloConstructsStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
// hello function responding to http requests
const helloFunc = new lambda.Function(this, 'HelloHandler', {
runtime: lambda.Runtime.NODEJS_12_X,
code: lambda.Code.fromAsset('lambda'),
handler: 'hello.handler'
});
// hit counter, aws-lambda-dynamodb pattern
const lambda_ddb_props: LambdaToDynamoDBProps = {
lambdaFunctionProps: {
code: lambda.Code.asset(`lambda`),
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'hitcounter.handler',
environment: {
DOWNSTREAM_FUNCTION_NAME: helloFunc.functionName
}
},
dynamoTableProps: {
tableName: 'Hits',
partitionKey: { name: 'path', type: dynamodb.AttributeType.STRING }
}
};
const hitcounter = new LambdaToDynamoDB(this, 'LambdaToDynamoDB', lambda_ddb_props);
// grant the hitcounter lambda role invoke permissions to the hello function
helloFunc.grantInvoke(hitcounter.lambdaFunction);
const api_lambda_props: ApiGatewayToLambdaProps = {
lambdaFunctionProps: {
code: lambda.Code.fromAsset('lambda'),
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'hello.handler'
},
apiGatewayProps: {
defaultMethodOptions: {
authorizationType: api.AuthorizationType.NONE
}
}
};
new ApiGatewayToLambda(this, 'ApiGatewayToLambda', api_lambda_props);
}
}
- Python
-
Editar o arquivohello_constructs/hello_constructs_stack.py
com o seguinte:
from aws_cdk import (
aws_lambda as _lambda,
aws_apigateway as apigw,
aws_dynamodb as ddb,
core,
)
from aws_solutions_constructs import (
aws_apigateway_lambda as apigw_lambda,
aws_lambda_dynamodb as lambda_ddb
)
class HelloConstructsStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# The code that defines your stack goes here
self.hello_func = _lambda.Function(
self, 'HelloHandler',
runtime=_lambda.Runtime.PYTHON_3_7,
handler='hello.handler',
code=_lambda.Code.asset('lambda'),
)
# hit counter, aws-lambda-dynamodb pattern
self.hit_counter = lambda_ddb.LambdaToDynamoDB(
self, 'LambdaToDynamoDB',
lambda_function_props=_lambda.FunctionProps(
runtime=_lambda.Runtime.PYTHON_3_7,
code=_lambda.Code.asset('lambda'),
handler='hitcounter.handler',
environment={
'DOWNSTREAM_FUNCTION_NAME': self.hello_func.function_name
}
),
dynamo_table_props=ddb.TableProps(
table_name='Hits',
partition_key={
'name': 'path',
'type': ddb.AttributeType.STRING
}
)
)
# grant the hitcounter lambda role invoke permissions to the hello function
self.hello_func.grant_invoke(self.hit_counter.lambda_function)
apigw_lambda.ApiGatewayToLambda(
self, 'ApiGatewayToLambda',
lambda_function_props=_lambda.FunctionProps(
runtime=_lambda.Runtime.PYTHON_3_7,
code=_lambda.Code.asset('lambda'),
handler='hello.handler',
),
api_gateway_props=apigw.RestApiProps(
default_method_options=apigw.MethodOptions(
authorization_type=apigw.AuthorizationType.NONE
)
)
)
Finalmente, precisamos atualizar nosso originalaws-apigateway-lambda
para utilizar nossa nova função Hit Counter que foi provisionada com oaws-lambda-dynamodb
Padrão acima.
- TypeScript
-
Editar o arquivolib/hello-constructs.ts
com o seguinte:
import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as api from '@aws-cdk/aws-apigateway';
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import { ApiGatewayToLambda, ApiGatewayToLambdaProps } from '@aws-solutions-constructs/aws-apigateway-lambda';
import { LambdaToDynamoDB, LambdaToDynamoDBProps } from '@aws-solutions-constructs/aws-lambda-dynamodb';
export class HelloConstructsStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
// hello function responding to http requests
const helloFunc = new lambda.Function(this, 'HelloHandler', {
runtime: lambda.Runtime.NODEJS_12_X,
code: lambda.Code.fromAsset('lambda'),
handler: 'hello.handler'
});
// hit counter, aws-lambda-dynamodb pattern
const lambda_ddb_props: LambdaToDynamoDBProps = {
lambdaFunctionProps: {
code: lambda.Code.asset(`lambda`),
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'hitcounter.handler',
environment: {
DOWNSTREAM_FUNCTION_NAME: helloFunc.functionName
}
},
dynamoTableProps: {
tableName: 'Hits',
partitionKey: { name: 'path', type: dynamodb.AttributeType.STRING }
}
};
const hitcounter = new LambdaToDynamoDB(this, 'LambdaToDynamoDB', lambda_ddb_props);
// grant the hitcounter lambda role invoke permissions to the hello function
helloFunc.grantInvoke(hitcounter.lambdaFunction);
const api_lambda_props: ApiGatewayToLambdaProps = {
existingLambdaObj: hitcounter.lambdaFunction,
apiGatewayProps: {
defaultMethodOptions: {
authorizationType: api.AuthorizationType.NONE
}
}
};
new ApiGatewayToLambda(this, 'ApiGatewayToLambda', api_lambda_props);
}
}
- Python
-
Editar o arquivohello_constructs/hello_constructs_stack.py
com o seguinte:
from aws_cdk import (
aws_lambda as _lambda,
aws_apigateway as apigw,
aws_dynamodb as ddb,
core,
)
from aws_solutions_constructs import (
aws_apigateway_lambda as apigw_lambda,
aws_lambda_dynamodb as lambda_ddb
)
class HelloConstructsStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# The code that defines your stack goes here
self.hello_func = _lambda.Function(
self, 'HelloHandler',
runtime=_lambda.Runtime.PYTHON_3_7,
handler='hello.handler',
code=_lambda.Code.asset('lambda'),
)
# hit counter, aws-lambda-dynamodb pattern
self.hit_counter = lambda_ddb.LambdaToDynamoDB(
self, 'LambdaToDynamoDB',
lambda_function_props=_lambda.FunctionProps(
runtime=_lambda.Runtime.PYTHON_3_7,
code=_lambda.Code.asset('lambda'),
handler='hitcounter.handler',
environment={
'DOWNSTREAM_FUNCTION_NAME': self.hello_func.function_name
}
),
dynamo_table_props=ddb.TableProps(
table_name='Hits',
partition_key={
'name': 'path',
'type': ddb.AttributeType.STRING
}
)
)
# grant the hitcounter lambda role invoke permissions to the hello function
self.hello_func.grant_invoke(self.hit_counter.lambda_function)
apigw_lambda.ApiGatewayToLambda(
self, 'ApiGatewayToLambda',
existing_lambda_obj=self.hit_counter.lambda_function,
api_gateway_props=apigw.RestApiProps(
default_method_options=apigw.MethodOptions(
authorization_type=apigw.AuthorizationType.NONE
)
)
)
Review as alterações
Vamos construir nosso projeto e revisar as alterações em nossos recursos que acontecerão quando implantarmos isso:
npm run build
cdk diff
Nossa saída deve ser semelhante a esta:
Stack HelloConstructsStack
IAM Statement Changes
┌───┬───────────────────────────────────┬────────┬───────────────────────────────────┬────────────────────────────────────┬───────────┐
│ │ Resource │ Effect │ Action │ Principal │ Condition │
├───┼───────────────────────────────────┼────────┼───────────────────────────────────┼────────────────────────────────────┼───────────┤
│ + │ ${HelloHandler.Arn} │ Allow │ lambda:InvokeFunction │ AWS:${LambdaFunctionServiceRole} │ │
├───┼───────────────────────────────────┼────────┼───────────────────────────────────┼────────────────────────────────────┼───────────┤
│ + │ ${HelloHandler/ServiceRole.Arn} │ Allow │ sts:AssumeRole │ Service:lambda.amazonaws.com │ │
├───┼───────────────────────────────────┼────────┼───────────────────────────────────┼────────────────────────────────────┼───────────┤
│ + │ ${LambdaToDynamoDB/DynamoTable.Ar │ Allow │ dynamodb:BatchGetItem │ AWS:${LambdaFunctionServiceRole} │ │
│ │ n} │ │ dynamodb:BatchWriteItem │ │ │
│ │ │ │ dynamodb:DeleteItem │ │ │
│ │ │ │ dynamodb:GetItem │ │ │
│ │ │ │ dynamodb:GetRecords │ │ │
│ │ │ │ dynamodb:GetShardIterator │ │ │
│ │ │ │ dynamodb:PutItem │ │ │
│ │ │ │ dynamodb:Query │ │ │
│ │ │ │ dynamodb:Scan │ │ │
│ │ │ │ dynamodb:UpdateItem │ │ │
└───┴───────────────────────────────────┴────────┴───────────────────────────────────┴────────────────────────────────────┴───────────┘
IAM Policy Changes
┌───┬─────────────────────────────┬────────────────────────────────────────────────────────────────────────────────┐
│ │ Resource │ Managed Policy ARN │
├───┼─────────────────────────────┼────────────────────────────────────────────────────────────────────────────────┤
│ + │ ${HelloHandler/ServiceRole} │ arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole │
└───┴─────────────────────────────┴────────────────────────────────────────────────────────────────────────────────┘
(NOTE: There may be security-related changes not in this list. See https://github.com/aws/aws-cdk/issues/1299)
Resources
[+] AWS::IAM::Role HelloHandler/ServiceRole HelloHandlerServiceRole11EF7C63
[+] AWS::Lambda::Function HelloHandler HelloHandler2E4FBA4D
[+] AWS::DynamoDB::Table LambdaToDynamoDB/DynamoTable LambdaToDynamoDBDynamoTable53C1442D
[+] AWS::IAM::Policy LambdaFunctionServiceRole/DefaultPolicy LambdaFunctionServiceRoleDefaultPolicy126C8897
[~] AWS::Lambda::Function LambdaFunction LambdaFunctionBF21E41F
├─ [+] Environment
│ └─ {"Variables":{"DOWNSTREAM_FUNCTION_NAME":{"Ref":"HelloHandler2E4FBA4D"},"DDB_TABLE_NAME":{"Ref":"LambdaToDynamoDBDynamoTable53C1442D"}}}
├─ [~] Handler
│ ├─ [-] hello.handler
│ └─ [+] hitcounter.handler
└─ [~] DependsOn
└─ @@ -1,3 +1,4 @@
[ ] [
[+] "LambdaFunctionServiceRoleDefaultPolicy126C8897",
[ ] "LambdaFunctionServiceRole0C4CDE0B"
[ ] ]
Implantação cdk
Pronto para implantar?
cdk deploy
Saídas da pilha
Quando a implantação estiver concluída, você notará esta linha:
Outputs:
HelloConstructsStack.RestApiEndpoint0551178A = https://xxxxxxxxxx
.execute-api.us-east-1.amazonaws.com/prod/
Testar seu aplicativo
Vamos tentar acertar este ponto final com curl. Copie o URL e execute (seu prefixo e região provavelmente serão diferentes).
curl https://xxxxxxxxxx
.execute-api.us-east-1.amazonaws.com/prod/
O resultado deve ser semelhante ao seguinte:
Hello, AWS Solutions Constructs! You've hit /
Agora, vamos revisar oHits
Tabela do Amazon DynamoDB.
-
Acesse o console do DynamoDB.
-
Verifique se você está na Região onde criou a tabela.
-
SelectTabelasno painel de navegação e selecione a caixa de seleçãoHitsTabela INTO.
-
Abra a tabela e selecione “Itens”.
-
Você deve ver quantos hits você tem para cada caminho.
-
Tente acertar um novo caminho e atualize a exibição Itens. Você verá um novo item com umhits
contagem de um.
Se esta é a saída que você recebeu, seu aplicativo funciona!