AWS Utilities¶
AWS helper functions and utilities for CDK constructs.
IAM Utilities¶
iam_utils
¶
IAM utilities for building policy statements and granting permissions.
This module provides predefined IAM actions lists and helper functions for creating policy statements for various AWS services.
Note
The list of actions for each service is incomplete and based on project needs. A helpful resource to research actions is: https://www.awsiamactions.io/
Functions¶
grant_managed_policies
¶
Grant managed policies to an IAM role.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role
|
Optional[IRole]
|
The IAM role to grant policies to. If None, no action is taken. |
required |
*managed_policies
|
Union[str, ManagedPolicy]
|
Variable number of managed policies to grant. Can be policy names (str) or ManagedPolicy objects. |
()
|
Source code in src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
batch_policy_statement
¶
batch_policy_statement(
env_base: EnvBase | None = None,
actions: list[str] = BATCH_FULL_ACCESS_ACTIONS,
sid: str = "BatchReadWrite",
) -> PolicyStatement
Create an IAM policy statement for AWS Batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_base
|
Optional[EnvBase]
|
Environment base for resource prefix. Defaults to None (matches all). |
None
|
actions
|
List[str]
|
List of Batch actions to allow. Defaults to BATCH_FULL_ACCESS_ACTIONS. |
BATCH_FULL_ACCESS_ACTIONS
|
sid
|
str
|
Statement ID. Defaults to "BatchReadWrite". |
'BatchReadWrite'
|
Returns:
| Type | Description |
|---|---|
PolicyStatement
|
IAM policy statement for Batch resources. |
Source code in src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
dynamodb_policy_statement
¶
dynamodb_policy_statement(
env_base: EnvBase | None = None,
actions: list[str] = DYNAMODB_READ_WRITE_ACTIONS,
sid: str = "DynamoDBReadWrite",
) -> PolicyStatement
Create an IAM policy statement for DynamoDB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_base
|
Optional[EnvBase]
|
Environment base for resource prefix. Defaults to None (matches all). |
None
|
actions
|
List[str]
|
List of DynamoDB actions to allow. Defaults to DYNAMODB_READ_WRITE_ACTIONS. |
DYNAMODB_READ_WRITE_ACTIONS
|
sid
|
str
|
Statement ID. Defaults to "DynamoDBReadWrite". |
'DynamoDBReadWrite'
|
Returns:
| Type | Description |
|---|---|
PolicyStatement
|
IAM policy statement for DynamoDB tables. |
Source code in src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
ecs_policy_statement
¶
ecs_policy_statement(
actions: list[str] = ECS_READ_ACTIONS,
sid: str = "ECSDescribe",
) -> PolicyStatement
Create an IAM policy statement for ECS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actions
|
List[str]
|
List of ECS actions to allow. Defaults to ECS_READ_ACTIONS. |
ECS_READ_ACTIONS
|
sid
|
str
|
Statement ID. Defaults to "ECSDescribe". |
'ECSDescribe'
|
Returns:
| Type | Description |
|---|---|
PolicyStatement
|
IAM policy statement for ECS resources. |
Source code in src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
lambda_policy_statement
¶
lambda_policy_statement(
env_base: EnvBase | None = None,
actions: list[str] = LAMBDA_FULL_ACCESS_ACTIONS,
sid: str = "LambdaReadWrite",
) -> PolicyStatement
Create an IAM policy statement for Lambda.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_base
|
Optional[EnvBase]
|
Environment base for resource prefix. Defaults to None (matches all). |
None
|
actions
|
List[str]
|
List of Lambda actions to allow. Defaults to LAMBDA_FULL_ACCESS_ACTIONS. |
LAMBDA_FULL_ACCESS_ACTIONS
|
sid
|
str
|
Statement ID. Defaults to "LambdaReadWrite". |
'LambdaReadWrite'
|
Returns:
| Type | Description |
|---|---|
PolicyStatement
|
IAM policy statement for Lambda functions. |
Source code in src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
s3_policy_statement
¶
s3_policy_statement(
env_base: EnvBase | None = None,
actions: list[str] = S3_FULL_ACCESS_ACTIONS,
sid: str = "S3FullAccess",
) -> PolicyStatement
Create an IAM policy statement for S3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_base
|
Optional[EnvBase]
|
Environment base for resource prefix. Defaults to None (matches all). |
None
|
actions
|
List[str]
|
List of S3 actions to allow. Defaults to S3_FULL_ACCESS_ACTIONS. |
S3_FULL_ACCESS_ACTIONS
|
sid
|
str
|
Statement ID. Defaults to "S3FullAccess". |
'S3FullAccess'
|
Returns:
| Type | Description |
|---|---|
PolicyStatement
|
IAM policy statement for S3 buckets. |
Source code in src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
secretsmanager_policy_statement
¶
secretsmanager_policy_statement(
actions: list[str] = SECRETSMANAGER_READ_ONLY_ACTIONS,
sid: str = "SecretsManagerReadOnly",
resource_id: str = "*",
region: str = None,
account: str = None,
) -> PolicyStatement
Create an IAM policy statement for Secrets Manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actions
|
List[str]
|
List of Secrets Manager actions to allow. Defaults to SECRETSMANAGER_READ_ONLY_ACTIONS. |
SECRETSMANAGER_READ_ONLY_ACTIONS
|
sid
|
str
|
Statement ID. Defaults to "SecretsManagerReadOnly". |
'SecretsManagerReadOnly'
|
resource_id
|
str
|
Resource identifier. Defaults to "*". |
'*'
|
region
|
str
|
AWS region. Defaults to None (current region). |
None
|
account
|
str
|
AWS account ID. Defaults to None (current account). |
None
|
Returns:
| Type | Description |
|---|---|
PolicyStatement
|
IAM policy statement for Secrets Manager resources. |
Source code in src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
ses_policy_statement
¶
ses_policy_statement(
actions: list[str] = SES_FULL_ACCESS_ACTIONS,
sid: str = "SESFullAccess",
) -> PolicyStatement
Create an IAM policy statement for SES.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actions
|
List[str]
|
List of SES actions to allow. Defaults to SES_FULL_ACCESS_ACTIONS. |
SES_FULL_ACCESS_ACTIONS
|
sid
|
str
|
Statement ID. Defaults to "SESFullAccess". |
'SESFullAccess'
|
Returns:
| Type | Description |
|---|---|
PolicyStatement
|
IAM policy statement for SES resources. |
Source code in src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
sfn_policy_statement
¶
sfn_policy_statement(
env_base: EnvBase | None = None,
actions: list[str] = SFN_STATES_READ_ACCESS_ACTIONS,
sid: str = "SfnFullAccess",
) -> PolicyStatement
Create an IAM policy statement for Step Functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_base
|
Optional[EnvBase]
|
Environment base for resource prefix. Defaults to None (matches all). |
None
|
actions
|
List[str]
|
List of Step Functions actions to allow. Defaults to SFN_STATES_READ_ACCESS_ACTIONS. |
SFN_STATES_READ_ACCESS_ACTIONS
|
sid
|
str
|
Statement ID. Defaults to "SfnFullAccess". |
'SfnFullAccess'
|
Returns:
| Type | Description |
|---|---|
PolicyStatement
|
IAM policy statement for Step Functions resources. |
Source code in src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
sns_policy_statement
¶
sns_policy_statement(
actions: list[str] = SNS_FULL_ACCESS_ACTIONS,
sid: str = "SNSFullAccess",
) -> PolicyStatement
Create an IAM policy statement for SNS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actions
|
List[str]
|
List of SNS actions to allow. Defaults to SNS_FULL_ACCESS_ACTIONS. |
SNS_FULL_ACCESS_ACTIONS
|
sid
|
str
|
Statement ID. Defaults to "SNSFullAccess". |
'SNSFullAccess'
|
Returns:
| Type | Description |
|---|---|
PolicyStatement
|
IAM policy statement for SNS resources. |
Source code in src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
ssm_policy_statement
¶
ssm_policy_statement(
actions: list[str] = SSM_READ_ACTIONS,
sid: str = "SSMParamReadActions",
) -> PolicyStatement
Create an IAM policy statement for SSM Parameter Store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actions
|
List[str]
|
List of SSM actions to allow. Defaults to SSM_READ_ACTIONS. |
SSM_READ_ACTIONS
|
sid
|
str
|
Statement ID. Defaults to "SSMParamReadActions". |
'SSMParamReadActions'
|
Returns:
| Type | Description |
|---|---|
PolicyStatement
|
IAM policy statement for SSM resources. |
Source code in src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
sqs_policy_statement
¶
sqs_policy_statement(
env_base: EnvBase | None = None,
actions: list[str] = SQS_FULL_ACCESS_ACTIONS,
sid: str = "SQSFullAccess",
) -> PolicyStatement
Create an IAM policy statement for SQS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_base
|
Optional[EnvBase]
|
Environment base for resource prefix. Defaults to None (matches all). |
None
|
actions
|
List[str]
|
List of SQS actions to allow. Defaults to SQS_FULL_ACCESS_ACTIONS. |
SQS_FULL_ACCESS_ACTIONS
|
sid
|
str
|
Statement ID. Defaults to "SQSFullAccess". |
'SQSFullAccess'
|
Returns:
| Type | Description |
|---|---|
PolicyStatement
|
IAM policy statement for SQS queues. |
Source code in src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
Step Functions Utilities¶
sfn_utils
¶
Step Functions utilities for JSON path references.
This module provides utilities for working with Step Functions JSON path expressions.
Classes¶
JsonReferencePath
¶
Bases: str
String extension for defining JsonPath reference expressions.
Provides properties and methods for working with JSON path references in AWS Step Functions state machines.
More details: https://github.com/json-path/JsonPath
Primarily supports "$" reference.
Example
path = JsonReferencePath("input.data") path.as_reference '$.input.data' path.as_key 'input.data.$'
Attributes¶
as_key
property
¶
Return the reference path as a key.
Appends ".$" suffix for use as a state machine key.
Returns:
| Type | Description |
|---|---|
str
|
The path formatted as a key. |
as_reference
property
¶
Return the reference path as a value.
Prepends "$." prefix for use as a state machine reference.
Returns:
| Type | Description |
|---|---|
str
|
The path formatted as a reference. |
as_jsonpath_string
property
¶
Return the path as a Step Functions string reference.
Returns:
| Type | Description |
|---|---|
str
|
The path wrapped in JsonPath.string_at(). |
as_jsonpath_object
property
¶
Return the path as a Step Functions object reference.
Returns:
| Type | Description |
|---|---|
IResolvable
|
The path wrapped in JsonPath.object_at(). |
as_jsonpath_json_to_string
property
¶
Return the path as a JSON-to-string conversion.
Returns:
| Type | Description |
|---|---|
str
|
The object reference converted to string via JsonPath.json_to_string(). |
as_jsonpath_list
property
¶
Return the path as a Step Functions list reference.
Returns:
| Type | Description |
|---|---|
list[str]
|
The path wrapped in JsonPath.list_at(). |
as_jsonpath_number
property
¶
Return the path as a Step Functions number reference.
Returns:
| Type | Description |
|---|---|
int | float
|
The path wrapped in JsonPath.number_at(). |
Functions¶
__new__
¶
Create a new JsonReferencePath.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The path content to wrap. |
required |
Returns:
| Type | Description |
|---|---|
|
A sanitized JsonReferencePath instance. |
Source code in src/aibs_informatics_cdk_lib/common/aws/sfn_utils.py
__add__
¶
Concatenate paths with a period separator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
The path segment to append. |
required |
Returns:
| Type | Description |
|---|---|
|
A new JsonReferencePath with the appended segment. |
Source code in src/aibs_informatics_cdk_lib/common/aws/sfn_utils.py
extend
¶
extend(*paths: str) -> JsonReferencePath
Extend the path with multiple segments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*paths
|
str
|
Variable number of path segments to append. |
()
|
Returns:
| Type | Description |
|---|---|
JsonReferencePath
|
A new JsonReferencePath with all segments appended. |
Source code in src/aibs_informatics_cdk_lib/common/aws/sfn_utils.py
sanitize
classmethod
¶
Sanitize a string for use as a JSON path.
Ensures string has non-consecutive periods and no periods at edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The string to sanitize. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The sanitized string. |
Source code in src/aibs_informatics_cdk_lib/common/aws/sfn_utils.py
is_reference
classmethod
¶
Check if a value is a JSON path reference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
Any
|
The value to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the value is a JsonReferencePath or starts with "$". |
Source code in src/aibs_informatics_cdk_lib/common/aws/sfn_utils.py
empty
classmethod
¶
empty() -> JsonReferencePath
Create an empty JsonReferencePath.
Returns:
| Type | Description |
|---|---|
JsonReferencePath
|
An empty JsonReferencePath instance. |
Core Utilities¶
core_utils
¶
Core AWS utility functions for building ARNs.
This module provides functions for constructing AWS ARNs for various services.
Functions¶
build_arn
¶
build_arn(
partition: str = "aws",
service: str | None = None,
region: str | None = None,
account: str | None = None,
resource_id: str | None = None,
resource_type: str | None = None,
resource_delim: Literal["/", ":"] = ":",
) -> str
Build an AWS ARN string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
partition
|
str
|
AWS partition. Defaults to "aws". |
'aws'
|
service
|
Optional[str]
|
AWS service name. Defaults to "*". |
None
|
region
|
Optional[str]
|
AWS region. Defaults to current region. |
None
|
account
|
Optional[str]
|
AWS account ID. Defaults to current account. |
None
|
resource_id
|
Optional[str]
|
Resource identifier. Defaults to "*". |
None
|
resource_type
|
Optional[str]
|
Resource type prefix. |
None
|
resource_delim (Literal["/", "
|
"]): Delimiter between type and ID. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The constructed ARN string. |
Source code in src/aibs_informatics_cdk_lib/common/aws/core_utils.py
build_batch_arn
¶
build_batch_arn(
region: str | None = None,
account: str | None = None,
resource_id: str | None = None,
resource_type: Literal[
"compute-environment",
"job",
"job-definition",
"job-queue",
]
| None = None,
) -> str
Build an AWS Batch ARN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Optional[str]
|
AWS region. |
None
|
account
|
Optional[str]
|
AWS account ID. |
None
|
resource_id
|
Optional[str]
|
Resource identifier. |
None
|
resource_type
|
Optional[Literal[...]]
|
Batch resource type. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The constructed Batch ARN string. |
Source code in src/aibs_informatics_cdk_lib/common/aws/core_utils.py
build_dynamodb_arn
¶
build_dynamodb_arn(
region: str | None = None,
account: str | None = None,
resource_id: str | None = None,
resource_type: Literal["table"] | None = None,
) -> str
Build an AWS DynamoDB ARN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Optional[str]
|
AWS region. |
None
|
account
|
Optional[str]
|
AWS account ID. |
None
|
resource_id
|
Optional[str]
|
Table name or resource identifier. |
None
|
resource_type
|
Optional[Literal['table']]
|
DynamoDB resource type. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The constructed DynamoDB ARN string. |
Source code in src/aibs_informatics_cdk_lib/common/aws/core_utils.py
build_ecr_arn
¶
build_ecr_arn(
region: str | None = None,
account: str | None = None,
resource_id: str | None = None,
resource_type: Literal["repository"] | None = None,
) -> str
Build an AWS ECR ARN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Optional[str]
|
AWS region. |
None
|
account
|
Optional[str]
|
AWS account ID. |
None
|
resource_id
|
Optional[str]
|
Repository name or resource identifier. |
None
|
resource_type
|
Optional[Literal['repository']]
|
ECR resource type. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The constructed ECR ARN string. |
Source code in src/aibs_informatics_cdk_lib/common/aws/core_utils.py
build_sfn_arn
¶
build_sfn_arn(
region: str | None = None,
account: str | None = None,
resource_id: str | None = None,
resource_type: Literal[
"*", "activity", "execution", "stateMachine"
]
| None = None,
) -> str
Build an AWS Step Functions ARN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Optional[str]
|
AWS region. |
None
|
account
|
Optional[str]
|
AWS account ID. |
None
|
resource_id
|
Optional[str]
|
State machine name or resource identifier. |
None
|
resource_type
|
Optional[Literal[...]]
|
Step Functions resource type. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The constructed Step Functions ARN string. |
Source code in src/aibs_informatics_cdk_lib/common/aws/core_utils.py
build_lambda_arn
¶
build_lambda_arn(
region: str | None = None,
account: str | None = None,
resource_id: str | None = None,
resource_type: Literal[
"function", "event-source-mapping", "layer"
]
| None = None,
) -> str
Build an AWS Lambda ARN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Optional[str]
|
AWS region. |
None
|
account
|
Optional[str]
|
AWS account ID. |
None
|
resource_id
|
Optional[str]
|
Function name or resource identifier. |
None
|
resource_type
|
Optional[Literal[...]]
|
Lambda resource type. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The constructed Lambda ARN string. |
Source code in src/aibs_informatics_cdk_lib/common/aws/core_utils.py
build_s3_arn
¶
build_s3_arn(
region: str | None = None,
account: str | None = None,
resource_id: str | None = None,
resource_type: Literal[
"bucket", "object", "accesspoint", "job"
]
| None = None,
) -> str
Build an AWS S3 ARN.
Note
S3 bucket and object ARNs do not include region or account.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Optional[str]
|
AWS region (ignored for bucket/object). |
None
|
account
|
Optional[str]
|
AWS account ID (ignored for bucket/object). |
None
|
resource_id
|
Optional[str]
|
Bucket name or object path. |
None
|
resource_type
|
Optional[Literal[...]]
|
S3 resource type. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The constructed S3 ARN string. |