Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ansible と AWS Lambda/cirasu_ansible_tettei_nyum...
Search
Yohei Kawahara
April 26, 2017
2
4.3k
Ansible と AWS Lambda/cirasu_ansible_tettei_nyumon_1
Ansible徹底入門 輪読もくもく会 #1 で話す予定の資料です.
Yohei Kawahara
April 26, 2017
Tweet
Share
More Decks by Yohei Kawahara
See All by Yohei Kawahara
20191002_CircleCIMeetupFukuoka#2
inokappa
1
780
kix_009_20190429
inokappa
0
780
2019-04-18 CircleCI Meetup Fukuoka #1
inokappa
2
7.9k
開発で使える AWS の API 操作を模倣する (OSS) ツールを探してみた/JAWS-UG Kagoshima Vol.8
inokappa
1
1.1k
隙間時間を使って exercism.io を始め隊 | リモートで勉強会 #2
inokappa
0
380
Congratulations fukuokarb#100
inokappa
0
3.8k
moto を 'もっと' 使おう/PyFukuoka#4
inokappa
0
4.3k
github-enterprise-user-kai-desuyone
inokappa
0
97
Elixir and AWS Tutorial
inokappa
0
520
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
502
140k
The Cost Of JavaScript in 2023
addyosmani
39
5.1k
Building Your Own Lightsaber
phodgson
101
5.9k
Building Better People: How to give real-time feedback that sticks.
wjessup
359
18k
Why Our Code Smells
bkeepers
PRO
333
56k
4 Signs Your Business is Dying
shpigford
179
21k
What the flash - Photography Introduction
edds
67
11k
Rails Girls Zürich Keynote
gr2m
93
13k
The Brand Is Dead. Long Live the Brand.
mthomps
53
37k
Testing 201, or: Great Expectations
jmmastey
35
6.9k
Build The Right Thing And Hit Your Dates
maggiecrowley
29
2.2k
Become a Pro
speakerdeck
PRO
22
4.8k
Transcript
Ansible ͱ AWS Lambda CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1 1 CIRASUʢ͠Β͢ʣAnsible
పఈೖ ྠಡ͘͘ձ #1
͋Μͨ୭Ͷʁ • ݪ ༸ฏ(͔ͬͺͱݺΜͰͶ ! ) • iret גࣜձࣾ cloudpack
ࣄۀ෦ • AWS Πϯϑϥͷӡ༻อकΛੜۀͱ͠ ͓ͯΓ·͢ • twitter : @inokara • Facebook : inokappa • ͖ͳΨϯμϜ : మ݂ͷΦϧϑΣϯζ Ansible ·ͩ·ͩॳ৺ऀͰ͢... 2 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
CIRASU ͬͯͳΜͶʁ ԬͰ Infrastructure as code Configuration as code
ɺSite Reliability Engineering (SRE)ɺDevOps ͳͲΠϯϑϥٕज़ऀΛऔΓר ٕ͘ज़ɾӡ༻ʹ͍ͭͯͷใΛ ΏΔʙ͘ ڞ༗ɾษڧ͢Δίϛϡχ ςΟͰ͢. 3 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
ࠓͷ͠ͳΜͶʁ Ansible పఈೖ 7 ষΛಡΜͰ͍ͯ... • Ansible Ͱ AWS Ϧιʔε͕͍͡ΕΔͷ͔ʂ
ͱ͍͏͜ͱͰɺAWS Lambda ͷσϓϩΠߋ৽࡞ۀʹ͑ͳ͍͔ ࢼߦࡨޡͯ͠Έ·ͨ͠. 4 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
ͰɺͲΜͳ͠Λͬ͢ͱʁ 1. AWS Lambda ΛσϓϩΠ͢Δ࣌ͷΈ 2. Ansible Ͱ Lambda Λૢ࡞ͯ͠Έͨ
ʙಋೖʙ 3. Ansible Ͱ Lambda Λૢ࡞ͯ͠Έͨ ʙ࣮ʙ 4. ͕࣌ؒ͋ͬͨΒ Demo 5. ·ͱΊ 5 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
ຊࢿྉͰར༻ͨ͠ڥ ຊࢿྉͰར༻ͨ͠ Python ٴͼ Ansible όʔδϣϯҎԼͷ௨Γ. $ python --version Python
2.7.13 $ ansible --version ansible 2.2.1.0 config file = /path/to/.ansible.cfg configured module search path = Default w/o overrides 6 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
AWS Lambda Λ σϓϩΠ͢Δ࣌ͷΈ 7 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
AWS Lambdaʁ • આ໌ෆཁʢͩͱࢥͬͯ·͢ʣ • αʔόϨείϯϐϡʔςΟϯάͷத֩Λ୲͏ΞϓϦέʔγϣϯ ࣮ߦڥΛఏڙ͢ΔαʔϏε • ΞϓϦέʔγϣϯΛ࣮ߦͨ࣌ؒ͠ͱ༻ϝϞϦ༰ྔͰ՝ۚ •
Node.js (JavaScript)ɺPythonɺ͓Αͼ Java (Java 8 ޓ)ɺͦͯ͠ C# Λαϙʔτ͍ͯ͠Δ ※ ҎԼɺAWS Lambda Lambda ͱهࡌ͠·͢. 8 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Lambda Λಈ͔͢·Ͱͷ todo 1. ؔ࿈ύοέʔδΛ४උ 2. ΞϓϦέʔγϣϯͱؔ࿈ύοέʔδΛ zip ͰݻΊΔ 3.
IAM Role ͷ࡞(ॳճͷΈ) 4. Lambda Function ͷ࡞(ॳճͷΈ) 5. zip ϑΝΠϧΛΞοϓϩʔυ 6. Πϕϯτ༻ͷݖݶΛ༩(ॳճͷΈɺඞཁͰ͋Ε) 9 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
͓ɺಈ͔͢ͷͬͯ ҙ֎ʹ໘ͩͳ͋ ※͋͘·ͰݸਓతͳݟղͰ͢. 10 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Lambda ͷཧπʔϧ ز͔ͭ OSS Ͱެ։͞Ε͍ͯΔ. • serverless/serverless(https:/ /github.com/serverless/serverless) • apex/apex(https:/
/github.com/apex/apex) • awslabs/chalice(https:/ /github.com/awslabs/chalice) • marcy-terui/lamvery(https:/ /github.com/marcy-terui/lamvery) • rackerlabs/lambda-uploader(https:/ /github.com/rackerlabs/ lambda-uploader) 11 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
͓ɺͲΕ͕ ͍͍ΜͩΖʁ ※खʹೃછΉͷΛ୳͢ఔ Lambda ͱͷؔΘΓ͕গͳ͍ͱ͍͏͜ͱ͋Δ͔ͳ... 12 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
apex ͱ͍͏ͷ͕ྑ͍ͱڭ͍͑ͯͨ ΜͰ͕͢ɺະͩࢼͤͣ. օ͞ΜͲͷ πʔϧΛར༻͞Ε͍ͯ·͢Ͱ͠ΐ͏ ͔ʁ 13 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ
#1
ͱ͍͏͜ͱͰɺࠓ·ͰཧπʔϧΛ ࢼ͢͜ͱΛͤͣɺͱΓ͋͑ͣͰ࡞ͬͨ Bash ͷԶԶσϓϩΠπʔϧΛ࡞ͬͯ ཧ(σϓϩΠ)͍ͯ͠·͕͢... 14 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
ԶԶσϓϩΠπʔϧͷݶք • ൚༻తʹ࡞Ε͍ͯͳ͍ͷͰɺLambda Function ຖʹγΣϧεΫϦ ϓτΛ༻ҙ͠ͳ͚Ε... • ࣗҎ֎ͷϝϯόʔʹ࿔ͬͯΒ͏͜ͱఆ͠ͳ͚Ε͍ ͚ͳ͍... •
ͦͦɺႈੑ͕৺(ႈੑΛ୲อ͠Α͏ͱ͢Δͱπʔϧͷ ίʔυྔ૿͑Δ) 15 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
ͱ͍͏͜ͱͰ... 16 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Ansible Ͱ Lambda Λૢ࡞ͯ͠Έͨ ʙಋೖʙ 17 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Ansible for AWS • AWS ϞδϡʔϧͰ AWS ͷ֤छϦιʔεΛૢ࡞͢Δ͜ͱ͕ग़དྷΔ • Ansible
2.2 Ͱ 80 छྨҎ্ͷϞδϡʔϧ͕༻ҙ͞Ε͍ͯΔ • EC2 વͷ͜ͱͳ͕ΒɺS3 Lambda IAM ૢ࡞Մೳʂ 18 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
AWS Ϟδϡʔϧͷ࢝Ίํ ඞཁͳͷҎԼͷ௨ΓͰ͢. • AWS SDK for Python(boto) • AWS
API Λૢ࡞ग़དྷΔ IAM Ϣʔβʔ༻ͷΞΫηεΩʔͱγʔΫ ϨοτΞΫηεΩʔ͘͠ IAM Role 19 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Playbook ͷ࣮ߦྫ(1) ڥม AWS_ACCESS_KEY_ID ʹೝূใΛಥͬࠐΉύλʔϯ. export AWS_ACCESS_KEY_ID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx export AWS_SECRET_ACCESS_KEY_ID=xxxxxxxxxxxxxxxxxxxxxxxxxx export
AWS_REGION=ap-northeast-1 ansible-playbook sample-playbook.yml Playbook ࣗମʹೝূํ๏ΛຒΊࠐΉํ๏͋Γ·͕͢ɺඇਪͩ ͱࢥ͍ͬͯ·͢. 20 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Playbook ͷ࣮ߦྫ(2) มʹ AWS_PROFILE ʹ Shared Credentials File ʹهࡌ͞Εͨ Profile
໊Λࢦఆ͢Δύλʔϯ. AWS_PROFILE=your-profile \ AWS_REGION=ap-northeast-1 \ ansible-playbook sample-playbook.yml దͳݖݶ͕༩͞Εͨ IAM Role ͕͍ͨ EC2 ͔Β࣮ߦ͢Δ߹ ʹɺڥมʹࢦఆ͢Δඞཁ͋Γ·ͤΜ. 21 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Playbook ͷྫ Lambda Function ࡞͢Δ Playbook ͷൈਮͰ͢. - name: Create
lambda function lambda: name: '{{ lambda_name }}' zip_file: '{{ function_dir }}/lambda.zip' handler: lambda_function.lambda_handler runtime: python2.7 role: '{{ role_arn }}' timeout: '{{ lambda_timeout }}' when: not ansible_check_mode 22 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Ansible Ͱ Lambda Λૢ࡞ͯ͠Έͨ ʙ࣮ʙ 23 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
ײँ Ұഋࢀߟʹ͖ͤͯ͞·ͨ͠. 24 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
sample Playbook Lambda Function ҎԼʹΞοϓ͍ͯ͠·͢. • https:/ /github.com/inokappa/ansible-sample-prj-lambda 25
CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Lambda Λಈ͔͢·Ͱͷ todo(࠶ܝ) 1. ؔ࿈ύοέʔδΛ४උ 2. ΞϓϦέʔγϣϯͱؔ࿈ύοέʔδΛ zip ͰݻΊΔ 3.
IAM Role ͷ࡞(ॳճͷΈ) 4. Lambda Function ͷ࡞(ॳճͷΈ) 5. zip ϑΝΠϧΛΞοϓϩʔυ 6. Πϕϯτ༻ͷݖݶΛ༩(ॳճͷΈɺඞཁͰ͋Ε) 26 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Role ͰͦΕͧΕΛׂ ग़དྷΔ͚ͩ൚༻తʹ͔ͨͬͨ͠ͷͰɺRole Ͱׂͯ͠Έ·ͨ͠ɻ $ tree roles roles !"" cloudwatch_event
# $"" tasks # $"" main.yml !"" iam # $"" tasks # $"" main.yml $"" lambda $"" tasks $"" main.yml 6 directories, 3 files 27 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Role Ͱར༻͢ΔมΛ Playbook ʹ - hosts: 127.0.0.1 connection: localhost roles:
- iam - lambda - cloudwatch_event vars: function_dir: '/path/to/sample' function_handler_name: 'sample.sample_handler' ... cloudwatch_event_schedule_expression: cron(* * * * ? *) ͜ͷ Playbook Lambda Function ຖʹ༻ҙ͢Δ͜ͱʹͳΓ·͢. 28 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
IAM Role ͷ Policy JSON Ͱཧ ! - name:
Create role iam_role: name: '{{ iam_role_name }}' assume_role_policy_document: "{{ lookup( 'file' , iam_role_policy_file ) }}" state: present - name: Attatch policy iam_policy: iam_type: role iam_name: '{{ iam_role_name }}' state: present policy_name: '{{ iam_policy_name }}' policy_json: "{{ lookup( 'file', iam_policy_file) }}" 29 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Ϟδϡʔϧ͕... ! • Facts Ϟδϡʔϧͷग़ྗΛղੳ͢Δͷ͕໘... • ϞδϡʔϧʹΑͬͯग़ྗϑΥʔϚοτ͕ҧ͏ͷπϥΠ • Lambda Function
IAM Role ͷ ARN ͚ͩΛऔಘ͢ΔϞδϡʔϧ ͕ແ͔ͬͨͷͰࣗ࡞ • Lambda Function Λ࡞ΔϞδϡʔϧ͕ݹ͔ͬͨ...(Function ͷߋ৽ ͕ग़དྷͳ͔ͬͨ) 30 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Demo ࢀߟʹ͍ͤͯͨ͞ Qiita هࣄΛਅࣅͯɺCloudWatch Event Ͱఆظ తʹ࣮ߦ͢Δ Lambda Function ΛσϓϩΠͯ͠Έ͍ͨͱࢥ͍·͢.
AWS_PROFILE=xxxxxxxx \ AWS_REGION=ap-northeast-1 \ ansible-playbook demo.yml 31 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
·ͱΊ 32 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Ansible Ͱ Lambda Λཧ͢Δ ! • Ansible ͷૢ࡞ײͦͷ··Ͱૢ࡞Մೳ • Lambda
ͷߏΛ YAML Ͱཧ͢Δ͜ͱ͕ग़དྷΔ • ԶԶཧπʔϧΑΓയવͱͨ҆͠৺ײ͕͋Δ • ଞͷཧπʔϧͱ͘ΒͯͲ͏ͳΜͩΖ... 33 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
Ansible Ͱ Lambda Λཧ͢Δࡍͷ ! • Ϟδϡʔϧ͕αϙʔτ͍ͯ͠ͳ͍ AWS Ϧιʔε͕͋Δ •
ԶԶϞδϡʔϧ AWS CLI Ͱิ͏ඞཁ͕͋Δ • ൚༻తʹ͠Α͏ͱ͢Ε͢Δఔ໘͍͘͞ • ສೳͰແ͍ • ग़དྷͳ͍ͱ͜Ζɺग़དྷΔٕज़ͰͳΜͱ͔͢Δ͔͠ͳ͍ 34 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
ࢀߟ • http:/ /www.shoeisha.co.jp/book/detail/9784798149943 ! • http:/ /qiita.com/kikusumk3/items/119bfb2da854c2b83791 ! 35
CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1
͓ΘΓ 36 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡ͘͘ձ #1