forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaTest.php
More file actions
109 lines (97 loc) · 3.37 KB
/
Copy pathLambdaTest.php
File metadata and controls
109 lines (97 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
namespace lambda\tests;
use Aws\Lambda\LambdaClient;
use Aws\S3\S3Client;
use Iam\IAMService;
use Lambda\LambdaService;
use PHPUnit\Framework\TestCase;
/**
* @group integ
*/
class LambdaTest extends TestCase
{
protected array $clientArgs;
protected LambdaClient $lambdaClient;
protected LambdaService $lambdaService;
protected IAMService $iamService;
protected S3Client $s3client;
public function setup(): void
{
echo "constructor";
$this->clientArgs = [
'region' => 'us-west-2',
'version' => 'latest',
'profile' => 'default',
];
$this->lambdaClient = new LambdaClient($this->clientArgs);
$this->lambdaService = new LambdaService();
$this->iamService = new IAMService();
$this->s3client = new S3Client($this->clientArgs);
}
public function testSingleActionCalls()
{
echo "start single action tests\n";
$uniqid = uniqid();
$code = __DIR__ . "/../lambda_handler_calculator.zip";
$functionName = "calculator-$uniqid";
$lambda_assume_role_policy = "{
\"Version\": \"2012-10-17\",
\"Statement\": [
{
\"Effect\": \"Allow\",
\"Principal\": {
\"Service\": \"lambda.amazonaws.com\"
},
\"Action\": \"sts:AssumeRole\"
}
]
}";
$bucketName = "amzn-s3-demo-bucket-$uniqid";
$this->s3client->createBucket([
'Bucket' => $bucketName,
]);
$file = file_get_contents($code);
$this->s3client->putObject([
'Bucket' => $bucketName,
'Key' => $functionName,
'Body' => $file,
]);
$handler = "lambda_handler_calculator";
$roleName = "test-lambda-role-$uniqid";
$role = $this->iamService->createRole($roleName, $lambda_assume_role_policy);
$this->iamService->attachRolePolicy(
$role['RoleName'],
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
);
$this->lambdaService->createFunction($functionName, $role, $bucketName, $handler);
$functionsList = $this->lambdaService->listFunctions();
foreach ($functionsList['Functions'] as $functionList) {
echo "{$functionList['FunctionName']}\n";
}
do {
$function = $this->lambdaService->getFunction($functionName);
} while ($function['Configuration']['State'] == 'Pending');
$params = [
'action' => 'plus',
'x' => 5,
'y' => 4,
];
$result = $this->lambdaService->invoke($functionName, $params);
//Clean up resources
$this->lambdaService->deleteFunction($functionName);
$this->iamService->deleteRole($roleName);
$deleteObjects = $this->s3client->listObjectsV2([
'Bucket' => $bucketName,
]);
$deleteObjects = $this->s3client->deleteObjects([
'Bucket' => $bucketName,
'Delete' => [
'Objects' => $deleteObjects['Contents'],
]
]);
$this->s3client->deleteBucket(['Bucket' => $bucketName]);
self::assertTrue(true);
}
}