Last active
January 15, 2020 07:53
-
-
Save cm-watanabeseigo/275f193a37c344cbdd7d857b98052bdb to your computer and use it in GitHub Desktop.
Sumo Logicのコレクターエージェントが導入済みのEC2を起動するCFnテンプレート + 起動スクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# usage: $0 <Access Log URL> <accessId> <accessKey> | |
function usage(){ | |
echo "Usage: ${0##*/} <Access Log URL> <accessId> <accessKey>" | |
echo "" | |
} | |
if [ "$3" = "" ]; then | |
usage | |
exit 1 | |
fi | |
CFN_TEMPLATE="sumologic-handson-ec2.yaml" | |
CFN_REGION="ap-northeast-1" | |
CFN_EC2InstanceProfile="**EC2に設定するIAMロール**" | |
CFN_EC2InstanceKeyName="**EC2に設定するキーペア名**" | |
CFN_EC2InstanceType="t3.nano" | |
STACK_NAME_PREFIX="sumologic-handson-launch-ec2" | |
INSTANCE_NAME_PREFIX="handson" | |
# 重複しないスタック名とEC2インスタンスのNameタグを生成 | |
mmdd=$(date +%m%d) | |
hash=$(ps -ef | md5sum | fold -w 6 | head -1) | |
stack_name="${STACK_NAME_PREFIX}-${mmdd}-${hash}" | |
instance_name="${INSTANCE_NAME_PREFIX}-${hash}" | |
# スクリプトの引数からパラメータをセット | |
access_log_url=$1 | |
sumologic_access_id=$2 | |
sumologic_access_key=$3 | |
# CFn スタック作成 | |
aws cloudformation create-stack \ | |
--region ${CFN_REGION} \ | |
--stack-name ${stack_name} \ | |
--parameters \ | |
ParameterKey=SumoLogicAccessId,ParameterValue=${sumologic_access_id} \ | |
ParameterKey=SumoLogicAccessKey,ParameterValue=${sumologic_access_key} \ | |
ParameterKey=AccessLogUrl,ParameterValue=${access_log_url} \ | |
ParameterKey=EC2InstanceName,ParameterValue=${instance_name} \ | |
ParameterKey=EC2InstanceProfile,ParameterValue=${CFN_EC2InstanceProfile} \ | |
ParameterKey=EC2InstanceKeyName,ParameterValue=${CFN_EC2InstanceKeyName} \ | |
ParameterKey=EC2InstanceType,ParameterValue=${CFN_EC2InstanceType} \ | |
--template-body file://${CFN_TEMPLATE} \ | |
--capabilities CAPABILITY_NAMED_IAM | |
printf "creating stack ( %s ) in progress..." "${stack_name}" && \ | |
aws cloudformation wait stack-create-complete \ | |
--stack-name ${stack_name} && \ | |
echo "done. " | |
# CFn ステータス | |
printf "Status: " | |
aws cloudformation describe-stacks \ | |
--stack-name ${stack_name} \ | |
--query 'Stacks[].StackStatus' \ | |
--output text | |
# 作成されたEC2インスタンスの情報表示 | |
printf "Instance ID: " | |
aws ec2 describe-instances \ | |
--filter Name=tag:aws:cloudformation:stack-name,Values=${stack_name} \ | |
--query 'Reservations[].Instances[].InstanceId' \ | |
--output text |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: 2010-09-09 | |
Parameters: | |
SumoLogicAccessId: | |
Description: "Access Id (14 character)" | |
Type: String | |
SumoLogicAccessKey: | |
Description: "Access Key (64 character)" | |
Type: String | |
SumoLogicName: | |
Description: "Collector Name" | |
Type: String | |
Default: "prod_webserver" | |
AccessLogUrl: | |
Description: "Apache Sample Access Log URL" | |
Type: String | |
SumoLogicInstallerUrl: | |
Type: String | |
Default: "https://collectors.jp.sumologic.com/rest/download/linux/64" | |
SumoLogicScriptName: | |
Type: String | |
Default: "SumoCollector.sh" | |
EC2InstanceName: | |
Type: String | |
Default: "handson" | |
EC2InstanceProfile: | |
Type: String | |
EC2InstanceKeyName: | |
Type: String | |
EC2InstanceType: | |
Type: String | |
Default: "t3a.nano" | |
EC2InstanceAMI: | |
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id> | |
Default: "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" | |
Resources: | |
EC2: | |
Type: AWS::EC2::Instance | |
Properties: | |
IamInstanceProfile: !Ref EC2InstanceProfile | |
ImageId: !Ref EC2InstanceAMI | |
InstanceType: !Ref EC2InstanceType | |
KeyName: !Ref EC2InstanceKeyName | |
Tags: | |
- Key: "Name" | |
Value: !Ref EC2InstanceName | |
- Key: "sumologic-access-id" | |
Value: !Ref SumoLogicAccessId | |
UserData: | |
Fn::Base64: !Sub | |
- | | |
#!/bin/bash -x | |
cd ${WORK_DIR} && \ | |
wget --no-verbose ${INSTALLER_URL} -O ${SCRIPT_NAME} && \ | |
chmod +x ${SCRIPT_NAME} && \ | |
sudo ./${SCRIPT_NAME} -q -console \ | |
-Vsumo.accessid=${ACCESS_ID} \ | |
-Vsumo.accesskey=${ACCESS_KEY} \ | |
-Vcollector.name=${NAME} \ | |
&& \ | |
sudo service collector start | |
curl -sSL -o ${WORK_DIR}/${LOG_NAME_LOCAL} \ | |
${ACCESS_LOG_URL} | |
- { | |
ACCESS_ID: !Ref SumoLogicAccessId, | |
ACCESS_KEY: !Ref SumoLogicAccessKey, | |
NAME: !Ref SumoLogicName, | |
INSTALLER_URL: !Ref SumoLogicInstallerUrl, | |
SCRIPT_NAME: !Ref SumoLogicScriptName, | |
ACCESS_LOG_URL: !Ref AccessLogUrl, | |
WORK_DIR: "/home/ec2-user", | |
LOG_NAME_LOCAL: "apache_access_logs_tutorial.txt", | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment