integration/resources/templates/combination/connector_restapi_to_function.yaml (129 lines of code) (raw):
Resources:
MyRole1:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service: lambda.amazonaws.com
ManagedPolicyArns:
- !Sub arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
MyRole2:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service: lambda.amazonaws.com
ManagedPolicyArns:
- !Sub arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
TriggerFunction:
Type: AWS::Lambda::Function
Properties:
Role: !GetAtt MyRole1.Arn
Runtime: nodejs18.x
Handler: index.handler
Code:
ZipFile: |
const https = require('https');
exports.handler = (event, context, callback) => {
const req = https.get(process.env.API_URL, res => {
var body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function() {
try {
const message = JSON.parse(Buffer.concat(body).toString())['message'];
if (message === "connector works!") {
callback(null, {
StatusCode: res.statusCode,
});
} else {
callback(new Error("Unexpected message"));
}
} catch(error) {
callback(error);
}
});
});
req.on('error', error => {
callback(error);
});
req.end();
};
Environment:
Variables:
API_URL: !Sub "https://${RestApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/prod/somepath"
ConnectedFunction:
Type: AWS::Lambda::Function
Properties:
Role: !GetAtt MyRole2.Arn
Runtime: nodejs18.x
Handler: index.handler
Code:
ZipFile: |
exports.handler = async (event) => {
return {
statusCode: 200,
body: JSON.stringify({
"message": "connector works!"
}),
};
};
RestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: !Sub "${AWS::StackName}-connector-restapi-to-function"
EndpointConfiguration:
# BJS and gov regions only support REGIONAL endpoints,
# using REGIONAL for all regions for simplicity.
Types:
- REGIONAL
Path:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref RestApi
ParentId: !GetAtt
- RestApi
- RootResourceId
PathPart: somepath
Method:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref RestApi
ResourceId: !Ref Path
HttpMethod: ANY
AuthorizationType: NONE
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub >-
arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ConnectedFunction.Arn}/invocations
RestAPIDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn:
- Method
Properties:
RestApiId: !Ref RestApi
RestAPIGatewayStage:
Type: AWS::ApiGateway::Stage
DependsOn:
- Method
Properties:
DeploymentId: !Ref RestAPIDeployment
RestApiId: !Ref RestApi
StageName: prod
MyConnector:
Type: AWS::Serverless::Connector
Properties:
Source:
Id: RestApi
Destination:
Id: ConnectedFunction
Permissions:
- Write
Metadata:
SamTransformTest: true