Skip to content

Instantly share code, notes, and snippets.

View filipeandre's full-sized avatar

Filipe Ferreira filipeandre

View GitHub Profile
@praveenc
praveenc / bedrock_utils_v2.py
Created June 14, 2025 16:01
Python util script to invoke Amazon Bedrock using ConverseAPI. Supports Document upload, Prompt Caching, Reasoning Enabled.
import re
import time
from dataclasses import dataclass
from functools import lru_cache
from pathlib import Path
from typing import Any, ClassVar, Final, Literal
import boto3
from botocore.config import Config
from botocore.exceptions import ClientError
@omriel1
omriel1 / main.py
Created April 16, 2025 08:54
Structured Output with Sonnet 3.7
from typing import Any
from dotenv import load_dotenv
from langchain_aws import ChatBedrockConverse
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnableLambda, RunnableParallel
from pydantic import BaseModel, Field
load_dotenv('.env')
@atheiman
atheiman / docker-image-share.sh
Created May 21, 2024 01:55
Package and share docker image
# Package docker image to .tar.gz to share to another machine
docker pull alpine
docker save alpine | gzip > alpine.tar.gz
# Load docker image from .tar.gz
docker load < alpine.tar.gz
# Show loaded image
docker image ls alpine
# REPOSITORY TAG IMAGE ID CREATED SIZE
---
Resources:
QueueForPipe:
Type: AWS::SQS::Queue
LambdaServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
@atheiman
atheiman / aws_switch_role_bookmark_generator.py
Last active June 30, 2025 21:03
AWS organization switch role (assume role) bookmark generator - outputs html to stdout that can be saved to a .html file and imported into browser bookmarks.
#!/usr/bin/env python3
import boto3
import os
# Example usage from a bash shell:
# PREFIX='AWS COMM' AWS_PROFILE=comm-mgmt ROLE_NAME=AWSControlTowerExecution python ./aws_switch_role_bookmark_generator.py > "./aws-switch-role-bookmarks.html"
# Environment variables for configuration
role_name = os.environ.get("ROLE_NAME", "OrganizationAccountAccessRole")
@atheiman
atheiman / security_hub_findings_query.py
Last active May 21, 2024 12:00
Security Hub findings querying and batch updating with boto3. Suppress sample findings (i.e. from GuardDuty "CreateSampleFindings").
#!/usr/bin/env python
import boto3
import json
sechub = boto3.client("securityhub")
sts = boto3.client("sts")
caller_arn = sts.get_caller_identity()["Arn"]
print(caller_arn)
@stekern
stekern / cfn-ddb-sfn.yml
Created November 12, 2023 16:12
CloudFormation template that demonstrates how to start a Step Functions State Machine using DynamoDB Streams and EventBridge Pipes
AWSTemplateFormatVersion: "2010-09-09"
Description: Creates a Step Functions State Machine that is executed when new items are added to a DynamoDB Table.
Resources:
Table:
Type: "AWS::DynamoDB::Table"
Properties:
AttributeDefinitions:
- AttributeName: "PK"
AttributeType: "S"
KeySchema:
@atheiman
atheiman / script.py
Last active October 14, 2024 21:01
Convert python dictionary of many levels to single level dictionary with dot notation keys. This can be useful when writing to a format that requires a flat object/dictionary, such as CSV.
def dict_dot_notation(d, path=[]):
d2 = {}
for k, v in d.items():
k_path = path + [str(k)]
k_formatted = ".".join(k_path)
if isinstance(v, dict):
# merge in dict with recursive call
d2 = {**d2, **dict_dot_notation(v, path=k_path)}
elif isinstance(v, list) or isinstance(v, tuple):
@phillip-le
phillip-le / assert-to-have-received-put-command-command.ts
Last active May 10, 2024 16:52
Unit Testing AWS SDK in TypeScript
it('should persist user to dynamodb', async () => {
await createUser(userInput);
expect(mockDynamoDbDocumentClient).toHaveReceivedCommandWith(PutCommand, {
TableName: 'TestUserTable',
Item: userToCreate,
});
});
@harishtocode
harishtocode / json-parse-remove-null.js
Created April 4, 2023 09:16
remove null properties from a nested json object
/*Remove null properties from a nested json object*/
function parse(input) {
if (
input == null ||
(Array.isArray(input) && input.length == 0) ||
JSON.stringify(input) == "{}"
) {
return null;
}