Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
AWSとTerraform初心者がやってみたこと
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
masa-ita
December 21, 2019
Technology
540
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
AWSとTerraform初心者が やってみたこと
masa-ita
December 21, 2019
More Decks by masa-ita
See All by masa-ita
Ollamaを使ったLocal Language Model活用法
itagakim
1
250
Run Instant NeRF on Docker
itagakim
1
2.4k
3D Clustering and Metric Learning
itagakim
0
420
Cloud TPUの使い方〜BigBirdの日本語学習済みモデルを作る〜
itagakim
0
760
多言語学習済みモデルmT5とは?
itagakim
1
800
AWSのGPUを安く使ってTensorFlowモデルを訓練する方法
itagakim
0
430
最近の自然言語処理モデルの動向
itagakim
1
600
ディープラーニングで芸術はできるか?〜生成系ネットワークの進展〜
itagakim
0
390
IntroductionToTensorFlow2_0.pdf
itagakim
1
390
Other Decks in Technology
See All in Technology
OpenTelemetryのメトリクスをCloudWatchに送ってPromQLで見てみた
ota1022
0
120
Multica × 長期記憶:40個のミニプロジェクト管理
eiei114
1
230
20260912_スクラムにジェネラリストは必要か
ryugen04
0
310
Sigmaで作る業務アプリ
kazushiro_honma
0
110
GoCon2026 - Open Source, Open World
sanposhiho
3
3.1k
GuardDuty 検知対応を DevOps Agent で効率化しようとしている話 / GuardDuty Investigations with DevOps Agent
masahirokawahara
1
350
「重なり」は迎える側がつくる ― 人もAIエージェントも歓迎するプロダクトエンジニアリング ―
go0517go
PRO
0
280
アプリログインとWeb認証基盤をつなぐ ASWebAuthenticationSession 作法
shimastripe
1
190
多層防御と最⼩権限で実現する、安全なAIエージェント設計パターン
lycorptech_jp
PRO
1
250
Podは生きているのにGoだけが落ちる:GOGCとGOMEMLIMITで追うInvisible OOM Killの謎
tkc66buzz
1
500
プロダクト思考 × 基盤思考を AIで実現する Compound Engineering
tkc66buzz
1
310
AIオーケストレーションを活用した 開発ワークフローの設計と実践
bqnq
0
130
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Odyssey Design
rkendrick25
PRO
2
800
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
680
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
560
Paper Plane
katiecoart
PRO
3
53k
What's in a price? How to price your products and services
michaelherold
247
13k
My Coaching Mixtape
mlcsv
0
310
Designing for humans not robots
tammielis
254
26k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Typedesign – Prime Four
hannesfritz
42
3.2k
Fireside Chat
paigeccino
43
4k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
980
Transcript
AWSとTerraform初⼼者が やってみたこと 板垣 正敏@JAWS-UG Niigata 2019/12/21
⾃⼰紹介 • 板垣 正敏 • 県内IT企業の技術顧問(2020/3まで) • 中⼩企業診断⼠ • ⽇本Rubyの会
• 新潟オープンソース協会 • Python機械学習勉強会 in 新潟 • TensorFlow Users Group • @itagakim • https://github.com/masa-ita • AWS歴 2017年〜
システムの全体像
なんのためのシステムか? • 社内でのディープラーニング 実習のための環境 • GPUマシンをたくさん⽤意で きない • GPUインスタンスは使うとき だけ起動して、コストを抑え
たい • IPアドレスを意識せずに使い たい • 当初はGUIコンソールとSSHで ⼿作業で構築 • Dynamic DNSとLet’s Encryptで 構成 • 現在はELBとCertificate Managerを使⽤
AWS Lambda EC2起動・停⽌指⽰ Amazon API Gateway DDNS登録・削除 Amazon DynamoDB Amazon
EC2 Amazon Route 53 Application Load Balancer Amazon CloudWatch Amazon Elastic Block Store Amazon Elastic File System EC2起動・停⽌完了通知 EC2稼働定期通知 AWS Identity and Access Management Generic group Event (time-based) Event (event-based) Event (event-based) AWS Certificate Manager VPC WebHook API call API call API call
本題:Terraformとは?
Infrastructure AS Code • コードとしてのインフラ(構成) • 再現・再利⽤性 • 設定ミス・設定漏れの防⽌ •
GUIによらないドキュメント化 • バージョン管理
なぜTERRAFORM? • AWSならCloudFormationがあるが・・・ • Azure, GCP, オンプレ(Vmware, Nutanix, etc.):ワークフ ローの統⼀
• https://www.terraform.io/docs/providers/index.html
TERRAFORMのしくみ • terraform init • terraform validate • terraform plan
• terraform apply .tf .tfstate .tfplan Target System terraform plan terraform apply
TERRAFORMのインストール • プラットフォームごとにZIPファイルで提供 • シングルバイナリでパスが通っていればOK • MacOSならHomebrewでもインストール可能 • tfenvでバージョン管理も
構成例 Labmdaを⽤いたDynamic DNSの部分のみを紹介
プロバイダ設定 • クレデンシャルは環境変数か awscliの設定ファイルに保存 すべき • バージョンアップでの⾮互換 を防ぐため、Terraformやプロ バイダのバージョン指定が有 効
• 右の例では、.tfstateファイル をS3バケットに保存している (共同作業やリモート参照の ため) terraform { required_version = "0.12.17” backend "s3" { bucket = "tfstate-terraform-demo” key = "network/terraform.tfstate” region = "ap-northeast-1” } } provider "aws" { version = "~> 2.41” region = "ap-northeast-1” }
variable "name" {} variable "policy" {} variable "identifier" {} resource
"aws_iam_role" "default" { name = var.name assume_role_policy = data.aws_iam_policy_document.assume_role.json } data "aws_iam_policy_document" "assume_role" { statement { actions = ["sts:AssumeRole"] principals { type = "Service” identifiers = [var.identifier] } } } resource "aws_iam_policy" "default" { name = var.name policy = var.policy } resource "aws_iam_role_policy_attachment" "default" { role = aws_iam_role.default.name policy_arn = aws_iam_policy.default.arn } output "iam_role_arn" { value = aws_iam_role.default.arn } output "iam_role_name" { value = aws_iam_role.default.name } IAM設定⽤モジュール定義
data "aws_iam_policy_document" "for_ddns_lambda" { statement { effect = "Allow” actions
= ["ec2:Describe*"] resources = ["*"] } statement { effect = "Allow” actions = ["dynamodb:*"] resources = ["*"] } statement { effect = "Allow” actions = [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents” ] resources = ["*"] } statement { effect = "Allow” actions = ["route53:*"] resources = ["*"] } } module "role_for_ddns_lambda" { source = "./iam_role” name = "role_for_ddns_lambda” identifier = "lambda.amazonaws.com” policy = data.aws_iam_policy_document.for_ddns_lambda.json } Lambda関数⽤IAM定義
data "archive_file" "lambda_function" { type = "zip” source_dir = "lambda/function”
output_path = "lambda/upload/lambda_function.zip” } resource "aws_lambda_function" "ddns_lambda" { filename = data.archive_file.lambda_function.output_path function_name = "ddns_lambda” role = module.role_for_ddns_lambda.iam_role_arn handler = "union.lambda_handler” source_code_hash = data.archive_file.lambda_function.output_base64sha256 runtime = "python2.7” memory_size = 128 timeout = 120 } resource "aws_lambda_permission" "allow_cloudwatch" { statement_id = "AllowExecutionFromCloudWatch” action = "lambda:InvokeFunction” function_name = aws_lambda_function.ddns_lambda.function_name principal = "events.amazonaws.com” source_arn = aws_cloudwatch_event_rule.ec2_lambda_ddns_rule.arn } Lambda関数定義
resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16” enable_dns_support = true
enable_dns_hostnames = true tags = { Name = "main” } } resource "aws_subnet" "main" { vpc_id = aws_vpc.main.id cidr_block = "10.0.1.0/24” map_public_ip_on_launch = true availability_zone = "ap-northeast-1d” tags = { Name = "main” } } resource "aws_internet_gateway" "gw" { vpc_id = aws_vpc.main.id tags = { Name = "main” } } resource "aws_route_table" "public" { vpc_id = aws_vpc.main.id } resource "aws_route" "public" { route_table_id = aws_route_table.public.id gateway_id = aws_internet_gateway.gw.id destination_cidr_block = "0.0.0.0/0” } resource "aws_route_table_association" "public" { subnet_id = aws_subnet.main.id route_table_id = aws_route_table.public.id } resource "aws_vpc_dhcp_options" "aws_subdomain" { domain_name = "aws.rails.to” domain_name_servers = ["AmazonProvidedDNS"] tags = { Name = "aws_subdomain” } } output "vpc_id" { value = aws_vpc.main.id } output "main_subnet_id" { value = aws_subnet.main.id } VPC定義
variable "name" {} variable "vpc_id" {} variable "port" {} variable
"cidr_blocks" { type = list(string) } resource "aws_security_group" "default" { name = var.name vpc_id = var.vpc_id } resource "aws_security_group_rule" "ingress" { type = "ingress” from_port = var.port to_port = var.port protocol = "tcp” cidr_blocks = var.cidr_blocks security_group_id = aws_security_group.default.id } resource "aws_security_group_rule" "egress" { type = "egress” from_port = 0 to_port = 0 protocol = "-1” cidr_blocks = ["0.0.0.0/0"] security_group_id = aws_security_group.default.id } output "security_group_id" { value = aws_security_group.default.id } Security Group定義⽤モジュール
module "web_server_sg" { source = "./security_group” name = "web-sg” vpc_id
= data.terraform_remote_state.network.outputs.vpc_id port = 80 cidr_blocks = ["0.0.0.0/0"] } resource "aws_instance" "web" { ami = "ami-0c3fd0f5d33134a76” instance_type = "t3.nano” vpc_security_group_ids = [module.web_server_sg.security_group_id] subnet_id = data.terraform_remote_state.network.outputs.main_subnet_id user_data = <<EOF #!/bin/bash yum install -y httpd systemctl start httpd.service EOF tags = { ZONE = "aws.rails.to.” CNAME = "web.aws.rails.to.” } } Security Group と EC2 インスタンスの定義
まとめ
Terraform は強⼒なツール • 何よりも構成の再現性がありがたい • GUIが時々変わるクラウドには必須のツールか • プロビジョニングの機能はそれほど便利ではない(ファイル コピーとコマンド実⾏) •
モジュールの使い⽅(使いどころ)がよくわかってません • 最適なリソース分割やディレクトリ配置がまだわかってませ ん
参考図書 • 「実践Terraform」野村友規著 • オンデマンド / Kindle • https://www.amazon.co.jp/dp/4844378139
参照サイト • Terraform Documentation • https://www.terraform.io/docs/index.html • Lamdaを⽤いたDynamic DNSの参考事例 •
https://aws.amazon.com/jp/blogs/compute/building-a-dynamic-dns-for- route-53-using-cloudwatch-events-and-lambda/ • https://github.com/aws-samples/aws-lambda-ddns-function
おまけ: Python機械学習勉強会 in 新潟 Restart #10 やり ます • 2020年1⽉11⽇(⼟)
• 新潟市中央公⺠館(クロスパル) • 「(仮)ディープラーニングで芸術はできるか?〜⽣成系ネッ トワークの進展〜」(itagakim) • 「機械学習による動作認識」(Oh!No!さん) • 「産学連携セミナー報告とインフラWG設⽴の提案」 (haru_fujitaさん) • 「Deep Learningを⽤いた残存⻭の⾃動認識」(historoidさん)