Skip to content

Instantly share code, notes, and snippets.

Avatar

Joey Trang anhtv08

View GitHub Profile
View deploySam.sh
#!/usr/bin/env bash
bucket_name=joey-my-sam-app
project_dir=$HOME/working/cloud-computing/lambda-demos
# packaging application
sam package \
--output-template-file $project_dir/output/packaged.yaml \
--s3-bucket $bucket_name > /dev/null
View full_template.yml
AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::Serverless-2016-10-31'
#Parameters:
# LambdaExecutionRole:
Resources:
ruleEvaluationDev:
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: 'cost-controller-function'
View shutdown_ec2_instance.py
def shutdown_ec2_instance(ec2_client, instanceId):
log.info("shutting down instance-id :" + instanceId)
ec2_client.terminate_instances(
InstanceIds=[
instanceId
]
)
View validate_tag_name.py
def validate_tag_name(tags: List[str]):
required_tag_names: Sequence[str] = ['environment', 'owner', 'projectName', 'costCentre']
is_valid = True
for required_tag in required_tag_names:
if required_tag not in tags:
is_valid = False
break
return is_valid
View evaluate_ec2_instance.py
def evaluate_ec2_instance(ec2_client, event):
instance_id = event['detail']['instance-id']
instance_state = event['detail']['state']
'''
only perform this evaluation rule when starting an new instance
check if instance state is pending then try to perfrom evaluation
'''
if instance_state != 'pending':
log.info('current state : ' + instance_state)
View lambda_handler.py
def lambda_handler(event, context):
log.info(" evaluating tags of ec2 instance")
if not event:
log.info("event is not valid")
else:
evaluate_ec2_instance(ec2_client, event)
View template.yml
Resources:
ruleEvaluationDev:
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: 'cost-controller-function'
Description: 'Integrating lambda with Parameter Store'
Handler: 'cost_controller.cost_controller_handlers.lambda_handler'
Runtime: 'python3.6'
Role: 'arn:aws:iam::<your_account_id_here>:role/service-role/my-rule-evaluation-function'
Timeout: 5
View cost_controller_handlers.py
from typing import Sequence
import logging as log
import boto3
from typing import List
from botocore.exceptions import ClientError
from typing import Dict
ec2_client = boto3.client('ec2')
sns_client = boto3.client('sns')
View cqlstress-example.yaml
keyspace: perftesting
keyspace_definition:
CREATE KEYSPACE perftesting WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': 3};
table: users
table_definition:
CREATE TABLE users (
View cassandra_setup.txt
CASSANDRA_VERION=3.11
CASSANDRA_NODES=(
CASSANDRA_1 \
CASSANDRA_2 \
CASSANDRA_3
)
# setup cassandra 3.11
# assume you have docker installed.