clear-skies-aws 2.0.1__py3-none-any.whl → 2.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {clear_skies_aws-2.0.1.dist-info → clear_skies_aws-2.0.2.dist-info}/METADATA +1 -1
- clear_skies_aws-2.0.2.dist-info/RECORD +63 -0
- clearskies_aws/__init__.py +15 -0
- clearskies_aws/actions/__init__.py +15 -0
- clearskies_aws/actions/action_aws.py +135 -0
- clearskies_aws/actions/assume_role.py +115 -0
- clearskies_aws/actions/ses.py +203 -0
- clearskies_aws/actions/sns.py +61 -0
- clearskies_aws/actions/sqs.py +81 -0
- clearskies_aws/actions/step_function.py +73 -0
- clearskies_aws/backends/__init__.py +19 -0
- clearskies_aws/backends/backend.py +106 -0
- clearskies_aws/backends/dynamo_db_backend.py +609 -0
- clearskies_aws/backends/dynamo_db_condition_parser.py +325 -0
- clearskies_aws/backends/dynamo_db_parti_ql_backend.py +965 -0
- clearskies_aws/backends/sqs_backend.py +61 -0
- clearskies_aws/configs/__init__.py +0 -0
- clearskies_aws/contexts/__init__.py +23 -0
- clearskies_aws/contexts/cli_web_socket_mock.py +19 -0
- clearskies_aws/contexts/lambda_alb.py +76 -0
- clearskies_aws/contexts/lambda_api_gateway.py +77 -0
- clearskies_aws/contexts/lambda_api_gateway_web_socket.py +57 -0
- clearskies_aws/contexts/lambda_invocation.py +19 -0
- clearskies_aws/contexts/lambda_sns.py +18 -0
- clearskies_aws/contexts/lambda_sqs_standard_partial_batch.py +29 -0
- clearskies_aws/di/__init__.py +6 -0
- clearskies_aws/di/aws_additional_config_auto_import.py +37 -0
- clearskies_aws/di/inject/__init__.py +6 -0
- clearskies_aws/di/inject/boto3.py +15 -0
- clearskies_aws/di/inject/boto3_session.py +13 -0
- clearskies_aws/di/inject/parameter_store.py +15 -0
- clearskies_aws/endpoints/__init__.py +2 -0
- clearskies_aws/endpoints/secrets_manager_rotation.py +195 -0
- clearskies_aws/endpoints/simple_body_routing.py +41 -0
- clearskies_aws/input_outputs/__init__.py +21 -0
- clearskies_aws/input_outputs/cli_web_socket_mock.py +18 -0
- clearskies_aws/input_outputs/lambda_alb.py +53 -0
- clearskies_aws/input_outputs/lambda_api_gateway.py +123 -0
- clearskies_aws/input_outputs/lambda_api_gateway_web_socket.py +71 -0
- clearskies_aws/input_outputs/lambda_input_output.py +87 -0
- clearskies_aws/input_outputs/lambda_invocation.py +85 -0
- clearskies_aws/input_outputs/lambda_sns.py +79 -0
- clearskies_aws/input_outputs/lambda_sqs_standard.py +84 -0
- clearskies_aws/mocks/__init__.py +1 -0
- clearskies_aws/mocks/actions/__init__.py +6 -0
- clearskies_aws/mocks/actions/ses.py +34 -0
- clearskies_aws/mocks/actions/sns.py +29 -0
- clearskies_aws/mocks/actions/sqs.py +29 -0
- clearskies_aws/mocks/actions/step_function.py +32 -0
- clearskies_aws/models/__init__.py +0 -0
- clearskies_aws/models/web_socket_connection_model.py +182 -0
- clearskies_aws/secrets/__init__.py +13 -0
- clearskies_aws/secrets/additional_configs/__init__.py +62 -0
- clearskies_aws/secrets/additional_configs/iam_db_auth.py +39 -0
- clearskies_aws/secrets/additional_configs/iam_db_auth_with_ssm.py +96 -0
- clearskies_aws/secrets/additional_configs/mysql_connection_dynamic_producer_via_ssh_cert_bastion.py +80 -0
- clearskies_aws/secrets/additional_configs/mysql_connection_dynamic_producer_via_ssm_bastion.py +162 -0
- clearskies_aws/secrets/akeyless_with_ssm_cache.py +60 -0
- clearskies_aws/secrets/parameter_store.py +52 -0
- clearskies_aws/secrets/secrets.py +16 -0
- clearskies_aws/secrets/secrets_manager.py +96 -0
- clear_skies_aws-2.0.1.dist-info/RECORD +0 -4
- {clear_skies_aws-2.0.1.dist-info → clear_skies_aws-2.0.2.dist-info}/WHEEL +0 -0
- {clear_skies_aws-2.0.1.dist-info → clear_skies_aws-2.0.2.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from clearskies import Model
|
|
7
|
+
from clearskies.query import Query
|
|
8
|
+
from types_boto3_sqs import SQSClient
|
|
9
|
+
|
|
10
|
+
from clearskies_aws.backends import backend
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class SqsBackend(backend.Backend):
|
|
14
|
+
"""
|
|
15
|
+
SQS backend for clearskies.
|
|
16
|
+
|
|
17
|
+
There's not too much to this. Just set it on your model and set the table name equal to the SQS url.
|
|
18
|
+
|
|
19
|
+
This doesn't support setting message attributes. The SQS call is simple enough that if you need
|
|
20
|
+
those you may as well just invoke the boto3 SDK yourself.
|
|
21
|
+
|
|
22
|
+
Note that this is a *write-only* backend. Reading from an SQS queue is different enough from
|
|
23
|
+
the way that clearskies models works that it doesn't make sense to try to make those happen here.
|
|
24
|
+
|
|
25
|
+
See the SQS context in this library for processing your queue data.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
_sqs: SQSClient
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
def sqs(self) -> SQSClient:
|
|
32
|
+
if not hasattr(self, "_sqs"):
|
|
33
|
+
if not self.environment.get("AWS_REGION", True):
|
|
34
|
+
raise ValueError("To use SQS you must use set AWS_REGION in the .env file or an environment variable")
|
|
35
|
+
|
|
36
|
+
self._sqs = self.boto3.client("sqs", region_name=self.environment.get("AWS_REGION", True))
|
|
37
|
+
|
|
38
|
+
return self._sqs
|
|
39
|
+
|
|
40
|
+
def create(self, data: dict[str, Any], model: Model) -> dict[str, Any]:
|
|
41
|
+
self.sqs.send_message(
|
|
42
|
+
QueueUrl=model.destination_name(),
|
|
43
|
+
MessageBody=json.dumps(data),
|
|
44
|
+
)
|
|
45
|
+
return {**data}
|
|
46
|
+
|
|
47
|
+
def update(self, id: int | str, data: dict[str, Any], model: Model) -> dict[str, Any]:
|
|
48
|
+
raise ValueError("The SQS backend only supports the create operation")
|
|
49
|
+
|
|
50
|
+
def delete(self, id: int | str, model: Model) -> bool:
|
|
51
|
+
raise ValueError("The SQS backend only supports the create operation")
|
|
52
|
+
|
|
53
|
+
def count(self, query: Query) -> int:
|
|
54
|
+
raise ValueError("The SQS backend only supports the create operation")
|
|
55
|
+
|
|
56
|
+
def records(
|
|
57
|
+
self,
|
|
58
|
+
query: Query,
|
|
59
|
+
next_page_data: dict[str, str | int] | None = None,
|
|
60
|
+
) -> list[dict[str, Any]]:
|
|
61
|
+
raise ValueError("The SQS backend only supports the create operation")
|
|
File without changes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from clearskies_aws.contexts.cli_web_socket_mock import CliWebSocketMock
|
|
4
|
+
from clearskies_aws.contexts.lambda_alb import LambdaAlb
|
|
5
|
+
from clearskies_aws.contexts.lambda_api_gateway import LambdaApiGateway
|
|
6
|
+
from clearskies_aws.contexts.lambda_api_gateway_web_socket import (
|
|
7
|
+
LambdaApiGatewayWebSocket,
|
|
8
|
+
)
|
|
9
|
+
from clearskies_aws.contexts.lambda_invocation import LambdaInvocation
|
|
10
|
+
from clearskies_aws.contexts.lambda_sns import LambdaSns
|
|
11
|
+
from clearskies_aws.contexts.lambda_sqs_standard_partial_batch import (
|
|
12
|
+
LambdaSqsStandardPartialBatch,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"CliWebSocketMock",
|
|
17
|
+
"LambdaAlb",
|
|
18
|
+
"LambdaApiGateway",
|
|
19
|
+
"LambdaApiGatewayWebSocket",
|
|
20
|
+
"LambdaInvocation",
|
|
21
|
+
"LambdaSns",
|
|
22
|
+
"LambdaSqsStandardPartialBatch",
|
|
23
|
+
]
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from clearskies.contexts import cli
|
|
4
|
+
|
|
5
|
+
from clearskies_aws.input_outputs import CliWebSocketMock as CliWebSocketMockInputOutput
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class CliWebSocketMock(cli.Cli):
|
|
9
|
+
"""
|
|
10
|
+
Help assist with testing websockets locally.
|
|
11
|
+
|
|
12
|
+
The LambdaApiGatewayWebSocket context makes it easy to run websocket applications, but testing
|
|
13
|
+
these locally is literally impossible. This context provides a close analogue to the way
|
|
14
|
+
the LambdaApiGatewayWebSocket context works to give some testing capabilities when running
|
|
15
|
+
locally.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def __call__(self):
|
|
19
|
+
return self.execute_application(CliWebSocketMockInputOutput())
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from clearskies.contexts.context import Context
|
|
6
|
+
|
|
7
|
+
from clearskies_aws.input_outputs import LambdaAlb as LambdaAlbInputOutput
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class LambdaAlb(Context):
|
|
11
|
+
"""
|
|
12
|
+
Run a clearskies application in a lambda behind an application load balancer.
|
|
13
|
+
|
|
14
|
+
There's nothing special here: just build your application, use the LambdaAlb context in a standard AWS lambda
|
|
15
|
+
handler, and attach your lambda to an ALB. This generally expects that the ALB will forward all requests to
|
|
16
|
+
the clearskies application, which will therefore handle all routing. However, you can also use path-based
|
|
17
|
+
routing in your target group to forward some subset of requests to separate lambdas, each using this same
|
|
18
|
+
context. When you do this, keep in mind that AWS still passes along the full path (including the part handled
|
|
19
|
+
by the ALB), so you want to make sure that your clearskies application is configured with the full URL as well.
|
|
20
|
+
|
|
21
|
+
Per AWS norms, you should create the context in the "root" of your python application, and then invoke it
|
|
22
|
+
inside a standard lambda handler function. This will allow AWS to cache the full application, improving
|
|
23
|
+
performance. If you create and invoke the context inside of your lambda handler, it will effectively turn
|
|
24
|
+
off any caching. In addition, clearskies does a fair amount of configuration validation when you create the
|
|
25
|
+
context, so this work will be repeated on every call.
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
import clearskies
|
|
29
|
+
import clearskies_aws
|
|
30
|
+
from clearskies.validators import Required, Unique
|
|
31
|
+
from clearskies import columns
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class User(clearskies.Model):
|
|
35
|
+
id_column_name = "id"
|
|
36
|
+
backend = clearskies.backends.MemoryBackend()
|
|
37
|
+
|
|
38
|
+
id = columns.Uuid()
|
|
39
|
+
name = columns.String(validators=[Required()])
|
|
40
|
+
username = columns.String(
|
|
41
|
+
validators=[
|
|
42
|
+
Required(),
|
|
43
|
+
Unique(),
|
|
44
|
+
]
|
|
45
|
+
)
|
|
46
|
+
age = columns.Integer(validators=[Required()])
|
|
47
|
+
created_at = columns.Created()
|
|
48
|
+
updated_at = columns.Updated()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
application = clearskies_aws.contexts.LambdaAlb(
|
|
52
|
+
clearskies.endpoints.RestfulApi(
|
|
53
|
+
url="users",
|
|
54
|
+
model_class=User,
|
|
55
|
+
readable_column_names=["id", "name", "username", "age", "created_at", "updated_at"],
|
|
56
|
+
writeable_column_names=["name", "username", "age"],
|
|
57
|
+
sortable_column_names=["id", "name", "username", "age", "created_at", "updated_at"],
|
|
58
|
+
searchable_column_names=["id", "name", "username", "age", "created_at", "updated_at"],
|
|
59
|
+
default_sort_column_name="name",
|
|
60
|
+
)
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def lambda_handler(event, context):
|
|
65
|
+
return application(event, context)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Context for Callables
|
|
69
|
+
|
|
70
|
+
When using this context, two additional named arguments become available to any callables invoked by clearskies:
|
|
71
|
+
`event` and `context`. These correspond to the original `event` and `context` variables provided by AWS to
|
|
72
|
+
the lambda.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
def __call__(self, event: dict[str, Any], context: dict[str, Any]) -> Any: # type: ignore[override]
|
|
76
|
+
return self.execute_application(LambdaAlbInputOutput(event, context))
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from clearskies.contexts.context import Context
|
|
6
|
+
|
|
7
|
+
from clearskies_aws.input_outputs import LambdaApiGateway as LambdaApiGatewayInputOutput
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class LambdaApiGateway(Context):
|
|
11
|
+
"""
|
|
12
|
+
Run a clearskies application in a lambda behind an API Gateway (v1 or v2).
|
|
13
|
+
|
|
14
|
+
There's nothing special here: just build your application, use the LambdaApiGateway context in a standard AWS
|
|
15
|
+
lambda handler, and attach your lambda to an Api Gateway. Per AWS norms, you should create the context in
|
|
16
|
+
the "root" of your python application, and then invoke it inside a standard lambda handler function. This
|
|
17
|
+
will allow AWS to cache the full application, improving performance. If you create and invoke the context
|
|
18
|
+
inside of your lambda handler, it will effectively turn off any caching. In addition, clearskies does a fair
|
|
19
|
+
amount of configuration validation when you create the context, so this work will be repeated on every call.
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
import clearskies
|
|
23
|
+
import clearskies_aws
|
|
24
|
+
from clearskies.validators import Required, Unique
|
|
25
|
+
from clearskies import columns
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class User(clearskies.Model):
|
|
29
|
+
id_column_name = "id"
|
|
30
|
+
backend = clearskies.backends.MemoryBackend()
|
|
31
|
+
|
|
32
|
+
id = columns.Uuid()
|
|
33
|
+
name = columns.String(validators=[Required()])
|
|
34
|
+
username = columns.String(
|
|
35
|
+
validators=[
|
|
36
|
+
Required(),
|
|
37
|
+
Unique(),
|
|
38
|
+
]
|
|
39
|
+
)
|
|
40
|
+
age = columns.Integer(validators=[Required()])
|
|
41
|
+
created_at = columns.Created()
|
|
42
|
+
updated_at = columns.Updated()
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
application = clearskies_aws.contexts.LambdaApiGateway(
|
|
46
|
+
clearskies.endpoints.RestfulApi(
|
|
47
|
+
url="users",
|
|
48
|
+
model_class=User,
|
|
49
|
+
readable_column_names=["id", "name", "username", "age", "created_at", "updated_at"],
|
|
50
|
+
writeable_column_names=["name", "username", "age"],
|
|
51
|
+
sortable_column_names=["id", "name", "username", "age", "created_at", "updated_at"],
|
|
52
|
+
searchable_column_names=["id", "name", "username", "age", "created_at", "updated_at"],
|
|
53
|
+
default_sort_column_name="name",
|
|
54
|
+
)
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def lambda_handler(event, context):
|
|
59
|
+
return application(event, context)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Context for Callables
|
|
63
|
+
|
|
64
|
+
When using this context, a number of additional named arguments become available to any callables invoked by
|
|
65
|
+
clearskies:
|
|
66
|
+
|
|
67
|
+
1. `event`
|
|
68
|
+
2. `context`
|
|
69
|
+
3. `resource`
|
|
70
|
+
4. `stage`
|
|
71
|
+
5. `request_id`
|
|
72
|
+
6. `api_id`
|
|
73
|
+
7. `api_version` (v1 or v2)
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
def __call__(self, event: dict[str, Any], context: dict[str, Any]) -> Any: # type: ignore[override]
|
|
77
|
+
return self.execute_application(LambdaApiGatewayInputOutput(event, context))
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from clearskies.contexts.context import Context
|
|
6
|
+
|
|
7
|
+
from clearskies_aws.input_outputs import (
|
|
8
|
+
LambdaApiGatewayWebSocket as LambdaApiGatewayWebSocketInputOutput,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class LambdaApiGatewayWebSocket(Context):
|
|
13
|
+
"""
|
|
14
|
+
Run a clearskies application behind an API Gateway that is configured for use as a websocket.
|
|
15
|
+
|
|
16
|
+
Websockets work much differently than standard API endpoints. Most importantly, none of the standard HTTP
|
|
17
|
+
concepts exist. Websockets requests don't have any of:
|
|
18
|
+
|
|
19
|
+
1. URL Path
|
|
20
|
+
2. Query Parameters
|
|
21
|
+
3. HTTP Headers
|
|
22
|
+
4. Response Headers
|
|
23
|
+
5. An HTTP Response
|
|
24
|
+
|
|
25
|
+
So in short, everything works completely differently. The reason is because a websocket is a
|
|
26
|
+
two-way communication channel that's created over a TCP/IP connection. It does start with an HTTP request,
|
|
27
|
+
but this is a one time request when the communication channel is first created. Later messages (which
|
|
28
|
+
are where the bulk of the communication happens) travel over the already-open connection, so
|
|
29
|
+
the communication looks nothing like HTTP. Usually, the data traveling over this connection is
|
|
30
|
+
a JSON payload, and since the connection is already opened it doesn't have any of the metadata associated
|
|
31
|
+
with an HTTP request (hence the lack of url/query/headers). In addition, the communication is no longer
|
|
32
|
+
transactional - messages from the client to the server do not come with a direct response, and the server
|
|
33
|
+
can send messages to the client without needing the former to initiate the conversation.
|
|
34
|
+
|
|
35
|
+
Routing and authorization are usually handled in-band, which means that the routing parameters or authentication
|
|
36
|
+
data are added directly to the JSON body sent over the open connection. This often results in applications
|
|
37
|
+
having to handle such things themselves, since the typical standards of web frameworks won't match up. In
|
|
38
|
+
the case of routing with an API Gateway, it has its own suggested standard of setting a routekey where the
|
|
39
|
+
API gateway will check for an application-defined route parameter in the request body and use this to route
|
|
40
|
+
to an appropriate lambda. With clearskies, you can also use the `clearskies.endpoints.JsonParamEndpointGroup`
|
|
41
|
+
to accomplish the same.
|
|
42
|
+
|
|
43
|
+
With a websocket through API Gateway, headers are available during the `on_connect` phase, so you can always
|
|
44
|
+
perform authentication then and record the result with the connection id (which can be used much like a
|
|
45
|
+
session id). Otherwise, authentication is typically handled by including the authentication token in every
|
|
46
|
+
message payload.
|
|
47
|
+
|
|
48
|
+
### Sending Messages
|
|
49
|
+
|
|
50
|
+
An important part of using websockets is being able to manage and send messages to clients. To help with this,
|
|
51
|
+
there is a base model class in `clearskies_aws.models.WebSocketConnectionModel`. Check the documentation for
|
|
52
|
+
this class to understand how this is managed and see a "starter" websocket application.
|
|
53
|
+
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def __call__(self, event: dict[str, Any], context: dict[str, Any], url: str = "") -> Any: # type: ignore[override]
|
|
57
|
+
return self.execute_application(LambdaApiGatewayWebSocketInputOutput(event, context, url))
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from clearskies.authentication import Public
|
|
6
|
+
from clearskies.contexts.context import Context
|
|
7
|
+
|
|
8
|
+
from clearskies_aws.input_outputs import LambdaInvocation as LambdaInvocationInputOutput
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class LambdaInvocation(Context):
|
|
12
|
+
|
|
13
|
+
def __call__(self, event: dict[str, Any], context: dict[str, Any]) -> Any: # type: ignore[override]
|
|
14
|
+
return self.execute_application(
|
|
15
|
+
LambdaInvocationInputOutput(
|
|
16
|
+
event,
|
|
17
|
+
context,
|
|
18
|
+
)
|
|
19
|
+
)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from clearskies.authentication import Public
|
|
4
|
+
from clearskies.contexts.context import Context
|
|
5
|
+
|
|
6
|
+
from clearskies_aws.input_outputs import LambdaSns as LambdaSnsInputOutput
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class LambdaSns(Context):
|
|
10
|
+
def __call__(self, event, context, method=None, url=None):
|
|
11
|
+
if self.execute_application is None:
|
|
12
|
+
raise ValueError("Cannot execute LambdaSnsEvent context without first configuring it")
|
|
13
|
+
|
|
14
|
+
try:
|
|
15
|
+
return self.execute_application(LambdaSnsInputOutput(event, context, method=method, url=url))
|
|
16
|
+
except Exception as e:
|
|
17
|
+
print("Failed message " + event["Records"][0]["Sns"]["MessageId"] + ". Error error: " + str(e))
|
|
18
|
+
raise e
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import traceback
|
|
4
|
+
|
|
5
|
+
from clearskies.authentication import Public
|
|
6
|
+
from clearskies.contexts.context import Context
|
|
7
|
+
|
|
8
|
+
from clearskies_aws.input_outputs import LambdaSqsStandard as LambdaSqsStandardInputOutput
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class LambdaSqsStandardPartialBatch(Context):
|
|
12
|
+
def __call__(self, event, context, url="", method="POST"):
|
|
13
|
+
item_failures = []
|
|
14
|
+
for record in event["Records"]:
|
|
15
|
+
print("Processing message " + record["messageId"], record["body"])
|
|
16
|
+
try:
|
|
17
|
+
self.execute_application(
|
|
18
|
+
LambdaSqsStandardInputOutput(record["body"], event, context, url=url, method=method)
|
|
19
|
+
)
|
|
20
|
+
except Exception as e:
|
|
21
|
+
print("Failed message " + record["messageId"] + " being returned for retry. Error error: " + str(e))
|
|
22
|
+
traceback.print_tb(e.__traceback__)
|
|
23
|
+
item_failures.append({"itemIdentifier": record["messageId"]})
|
|
24
|
+
|
|
25
|
+
if item_failures:
|
|
26
|
+
return {
|
|
27
|
+
"batchItemFailures": item_failures,
|
|
28
|
+
}
|
|
29
|
+
return {}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
from types import ModuleType
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
import boto3 as boto3_module
|
|
6
|
+
from clearskies import Environment
|
|
7
|
+
from clearskies.di import AdditionalConfigAutoImport
|
|
8
|
+
from clearskies.di.additional_config import AdditionalConfig
|
|
9
|
+
|
|
10
|
+
from clearskies_aws.secrets import ParameterStore
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class AwsAdditionalConfigAutoImport(AdditionalConfigAutoImport):
|
|
14
|
+
"""
|
|
15
|
+
Provide a DI with AWS modules built-in.
|
|
16
|
+
|
|
17
|
+
This DI auto injects boto3, boto3 Session and the parameter store.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def provide_boto3(self) -> ModuleType:
|
|
21
|
+
import boto3
|
|
22
|
+
|
|
23
|
+
return boto3
|
|
24
|
+
|
|
25
|
+
def provide_parameter_store(self) -> ParameterStore:
|
|
26
|
+
# This is just here so that we can auto-inject the secrets into the environment without having
|
|
27
|
+
# to force the developer to define a secrets manager
|
|
28
|
+
return ParameterStore()
|
|
29
|
+
|
|
30
|
+
def provide_boto3_session(self, boto3: ModuleType, environment: Environment) -> boto3_module.session.Session:
|
|
31
|
+
if not environment.get("AWS_REGION", True):
|
|
32
|
+
raise ValueError(
|
|
33
|
+
"To use AWS Session you must use set AWS_REGION in the .env file or an environment variable"
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
session = boto3.session.Session(region_name=environment.get("AWS_REGION", True))
|
|
37
|
+
return session
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from types import ModuleType
|
|
4
|
+
|
|
5
|
+
from clearskies.di.injectable import Injectable
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Boto3(Injectable):
|
|
9
|
+
def __init__(self, cache: bool = True):
|
|
10
|
+
self.cache = cache
|
|
11
|
+
|
|
12
|
+
def __get__(self, instance, parent) -> ModuleType:
|
|
13
|
+
if instance is None:
|
|
14
|
+
return self # type: ignore
|
|
15
|
+
return self._di.build_from_name("boto3", cache=self.cache)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from types import ModuleType
|
|
2
|
+
|
|
3
|
+
from clearskies.di.injectable import Injectable
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Boto3Session(Injectable):
|
|
7
|
+
def __init__(self, cache: bool = True):
|
|
8
|
+
self.cache = cache
|
|
9
|
+
|
|
10
|
+
def __get__(self, instance, parent) -> ModuleType:
|
|
11
|
+
if instance is None:
|
|
12
|
+
return self # type: ignore
|
|
13
|
+
return self._di.build_from_name("boto3_session", cache=self.cache)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from clearskies.di.injectable import Injectable
|
|
2
|
+
|
|
3
|
+
from clearskies_aws.secrets.parameter_store import (
|
|
4
|
+
ParameterStore as ParameterStoreDependency,
|
|
5
|
+
)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ParameterStore(Injectable):
|
|
9
|
+
def __init__(self, cache: bool = True):
|
|
10
|
+
self.cache = cache
|
|
11
|
+
|
|
12
|
+
def __get__(self, instance, parent) -> ParameterStoreDependency:
|
|
13
|
+
if instance is None:
|
|
14
|
+
return self # type: ignore
|
|
15
|
+
return self._di.build_from_name("parameter_store", cache=self.cache)
|