Skip to content

Instantly share code, notes, and snippets.

@diogoaurelio
diogoaurelio / lambda_module_instantiation.tf
Last active Sep 16, 2018
lambda_module_instantiation.tf
View lambda_module_instantiation.tf
module "redshift_loader_lambda" {
source = "github.com/diogoaurelio/terraform-aws-lambda-module"
version = "v0.0.1"
aws_region = "${var.aws_region}"
environment = "${var.environment}"
project = "${var.project}"
lambda_unique_function_name = "${var.redshift_loader_lambda_unique_function_name}"
@diogoaurelio
diogoaurelio / redshift_lambda_data_loader.py
Last active Sep 1, 2018
Sample code tfor a S3 triggered AWS Lambda function to issue Copy command to load data into Redshift DB
View redshift_lambda_data_loader.py
import urllib
import pg8000
import boto3
import os
import logging
IAM_ROLE = os.environ['IAM_ROLE']
DB_NAME = os.environ['DB_NAME']
DB_USER = os.environ['DB_USER']
DB_PORT = os.environ['DB_PORT']
View scalaReceiverMethodTrait.scala
trait Actor {
final def sender(): ActorRef = context.sender()
}
View sillyScalaExample.scala
import akka.actor.Actor
import akka.actor.Props
import akka.event.Logging
class DudeA extends Actor {
val log = Logging(context.system, this)
def receive = {
case "hello" => sender() ! "Hallo, Grüß Dich!"
case _ => log.info("Hmm...")
View scalaActorTrait.scala
trait Actor {
// to make type Receive known in subclasses without import
type Receive = Actor.Receive
// [...]
//#receive
def receive: Actor.Receive
// [...]
}
View basicActorClass.scala
import akka.actor.Actor
import akka.actor.Props
import akka.event.Logging
class MyActor extends Actor {
val log = Logging(context.system, this)
var myMutatedVar: Int = 0
def receive = {
case "test" => log.info("received test")
case "mutate" => myMutatedVar += 1; log.info(s"Mutated var to: $myMutatedVar")
View iam.tf
variable "role_name" {}
variable "role_policy_file" {}
resource "aws_iam_role" "role" {
name = "${var.role_name}"
assume_role_policy = "${file("${path.module}/policies/${var.role_policy_file}")}"
}
output "role_arn" {
value = "${aws_iam_role.role.arn}"
View aws_instance_temp_credentials.py
def get_temp_credentials(role=DEFAULT_INSTANCE_ROLE):
""" Retrieves temp AWS credentials """
query_uri = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/{}'.format(role)
print('Querying AWS for credentials - {}'.format(query_uri))
try:
sts_credentials = requests.get(query_uri).json()
if isinstance(sts_credentials, dict) and \
sts_credentials.get('Code') == 'Success':
print('Successfully retrieved temp AWS credentials.')
return sts_credentials
View assume-role-policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
View aws_assume_role_sample.py
import boto3
def role_arn_to_session(**args):
"""
Usage :
session = role_arn_to_session(
RoleArn='arn:aws:iam::012345678901:role/example-role',
RoleSessionName='ExampleSessionName')
client = session.client('sqs')
"""