Skip to content

Commit 98bea8a

Browse files
committed
Advertise ServiceEndpoint class
1 parent b601fb4 commit 98bea8a

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,32 +142,35 @@ $ aws lambda update-function-configuration \
142142

143143
Using other AWS services
144144
------------------------
145-
In order to programmatically use other AWS services such as SQS with the [AWS SDK for PHP](https://docs.aws.amazon.com/sdk-for-php/index.html), pass the credentials via environment:
145+
In order to programmatically use other AWS services use the *ServiceEndpoint* class:
146146

147147
```php
148-
use Aws\Sqs\SqsClient;
149-
use Aws\Credentials\CredentialProvider;
148+
use com\amazon\aws\{Credentials, ServiceEndpoint};
150149
use com\amazon\aws\lambda\Handler;
151150

152-
class Trigger extends Handler {
151+
class Streaming extends Handler {
153152

154153
/** @return com.amazon.aws.lambda.Lambda|callable */
155154
public function target() {
156-
$queue= new SqsClient([
157-
'version' => '2012-11-05',
158-
'region' => $this->environment->variable('AWS_REGION'),
159-
'credentials' => CredentialProvider::env(),
160-
]);
161-
162-
// ...omitted for brevity...
155+
return function($event, $context) {
156+
157+
// Send message to WebSocket connection
158+
(new ServiceEndpoint('execute-api', $this->environment->credentials()))
159+
->in($context->region)
160+
->using($event['requestContext']['apiId'])
161+
->resource('/{stage}/@connections/{connectionId}', $event['requestContext'])
162+
->transmit(['message' => 'Reply'])
163+
;
164+
return ['statusCode' => 200];
165+
};
163166
}
164167
}
165168
```
166169

167170
To test this locally, pass the necessary environment variables via *-e* on the command line:
168171

169172
```bash
170-
$ xp lambda test -e AWS_ACCESS_KEY_ID=... -e AWS_SECRET_ACCESS_KEY=... Trigger
173+
$ xp lambda test -e AWS_ACCESS_KEY_ID=... -e AWS_SECRET_ACCESS_KEY=... Streaming '{"requestContext":...}'
171174
# ...
172175
```
173176

0 commit comments

Comments
 (0)