aws tools for power shellをつかいこなそう

41
AWS Tools for Windows PowerShellをつかいこなそう アマゾンデータサービスジャパン ソリューションアーキテクト 渡邉源太 2013/12/21

Upload: genta-watanabe

Post on 28-May-2015

2.296 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Aws tools for power shellをつかいこなそう

AWS Tools for Windows PowerShellをつかいこなそう

アマゾンデータサービスジャパンソリューションアーキテクト

渡邉源太

2013/12/21

Page 2: Aws tools for power shellをつかいこなそう

自己紹介(1/2)

名前• 渡邉源太

所属• ソリューションアーキテクト• アマゾンデータサービスジャパン株式会社

Twitter ID• @gentaw0

好きなAWSサービス• Amazon WorkSpaces

2

Page 3: Aws tools for power shellをつかいこなそう

自己紹介(自己紹介(自己紹介(自己紹介(2/2))))

Microsoft MVP for Virtualization - Virtual Machine(Jul 2007 - Jun 2010)Publications

� 日経BP すべてわかる仮想化大全• http://coin.nikkeibp.co.jp/coin/itpro/virtual/index.html

� @IT連載 三大仮想化ソフトウェア機能比較• http://www.atmarkit.co.jp/fwin2k/operation/indexpage/inde

x.html#vmcomp

Page 4: Aws tools for power shellをつかいこなそう

AWSサービスメニューお客様のアプリケーション

認証AWS IAM

モニタリングAmazon

CloudWatch

Web管理画⾯Management

Console

デプロイと自動化AWS Elastic Beanstalk

AWS CloudFromation

IDEプラグインEclipse

Visual Studio

ライブラリ & SDKsJava, PHP, .NET,

Python, Ruby

Development & Administration

AWS グローバルインフラGeographical Regions, Availability Zones, Points of PresenceAZRegion

ネットワーク & ルーティングAmazon VPC / Amazon Elastic Load Balancer / Amazon Route 53 /AWS Direct Connect

InfrastructureService

コンピュータ処理Amazon EC2Auto Scale

ストレージAmazon S3

Amazon EBSAmazon Glacier

AWS StorageGateway

データベースAmazon RDS

Amazon DynamoDBAmazon SimpleDB

コンテンツ配信Amazon CloudFront

メッセージAmazon SNSAmazon SQSAmazon SES

分散処理Elastic MapReduce

検索エンジンAmazon Cloud Search

キャッシングAmazon Elasticache

ワークフロー管理Amazon SWF

Application Service

Page 5: Aws tools for power shellをつかいこなそう

AWSのグローバルインフラストラクチャUS West(Northern

California)

US East(Northern

Virginia)

EU(Ireland)

Asia

Pacific(Singapore)

Asia

Pacific(Tokyo)

GovCloud(US ITAR

Region)

US West(Oregon)

South

America(Sao Paulo)

AWS RegionsAWS Edge Locations

1. どのリージョンでも同じ使い勝手、同じやり方で自由に利⽤可能

2. リージョンを指定して、アプリケーションやデータを配置する事が可能

Page 6: Aws tools for power shellをつかいこなそう

リージョンとアベイラビリティゾーン(AZ)

1リージョン内にAZ(データセンター群)が複数拠点存在AZはお互いに地理的・電源的・ネットワーク的に分離されているAZ間は高速専⽤線で接続(リージョン間はインターネット経由)

1リージョン内にAZ(データセンター群)が複数拠点存在AZはお互いに地理的・電源的・ネットワーク的に分離されているAZ間は高速専⽤線で接続(リージョン間はインターネット経由)

Page 7: Aws tools for power shellをつかいこなそう

Amazon Web Servicesとマイクロソフト

Windows on AWSがもたらすお客様のビジネスへのメリット

AWS上で利⽤可能なWindows製品

新しいハードウェアは不要 テスト環境をオンデマンドに

数分で利⽤開始

迅速なパフォーマンス

既存のMicrosoftライセンスを使⽤

ライセンスモビリティ

MicrosoftWindows Server

Microsoft SQL Server

MicrosoftSharePoint

Page 8: Aws tools for power shellをつかいこなそう

Amazon EC2 - Windowsインスタンス

Amazon EC2で実⾏可能なWindowsイメージ以下のバージョンのWindowsが利⽤可能• Windows Server 2012• Windows Server 2008 R2• Windows Server 2008• Windows Server 2003 R2

Windowsのバージョンに応じたSQL Server導入済みイメージもご⽤意• SQL Server Express Edition(無料)• SQL Server Web Edition(時間課⾦)• SQL Server Standard Edition(時間課⾦)

Page 9: Aws tools for power shellをつかいこなそう

SQL Server AlwaysOn可用性グループon AWS

Windows Server Failover Cluster ManagerおよびSQL Server 2012 Availability GroupをAWS上で構成可能に!Availability Zoneをまたいだレプリケーションと高可⽤性を実現環境構築のためのCloudformationテンプレートも提供

9

Availability Zone Availability Zone

SQL Server DB

SQL Server DB

Domain Controller

Domain Controller

AWS Cloud

Page 10: Aws tools for power shellをつかいこなそう

Amazon RDS for SQL Server

標準機能• SQL Server Migration Assistant• Database Engine Tuning Advisor (SE, EE, Web)• SQL Server Agent• SSL接続• Transparent Data Encryption(TDE)

バージョンアップ• 2008 R2から 2012へのメジャーバージョンアップ機能

詳細情報• 機能情報

• http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/SQLServer.Concepts.General.html

• FAQ• http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/RD

SFAQ.SQLServer.html

Page 11: Aws tools for power shellをつかいこなそう

CloudFormationによるSharePointサーバーファーム構築

CloudFormationテンプレートのサンプルを使⽤して6ステップでSharePointサーバーファームを構築• http://aws.amazon.com/articles/9982940049271604

テンプレートは環境にあわせて自由にカスタマイズ可能コストは約$3250/月

Page 12: Aws tools for power shellをつかいこなそう

AWSのサービス操作と言えば

AWS Management ConsoleAWS Management Console

AWSコマンドラインツールAWSコマンドラインツール

Page 13: Aws tools for power shellをつかいこなそう

マネージメントコンソールだけでなく、APIからプログラマブルにアクセス

ec2-run-instances ami-b232d0db

--instance-count 3

--availability-zone

ap-northeast-1a

--instance-type m1.small

ec2-run-instances ami-b232d0db

--instance-count 3

--availability-zone

ap-northeast-1b

--instance-type m1.small

Tokyo リージョン

Page 14: Aws tools for power shellをつかいこなそう

動作イメージ

起動、停止

Put,Get

DB起動バックアップ

情報取得

REST

SOAPAWS SDK

認証情報ロギング

Page 15: Aws tools for power shellをつかいこなそう

認証情報AWSマネジメントコンソールのIAMタブで認証情報作成• アクセスキー、シークレットキ-、(optional:MFA)• シークレットキーは作成時のみ取得可能

Page 16: Aws tools for power shellをつかいこなそう

操作の種類

例:EC2• インスタンス起動・・・RunInstances• リブート・・・ RebootInstances• IPアドレス付与・・AllocateAddress• などさまざまな操作が、プログラムから実⾏可能• http://docs.amazonwebservices.com/AWSEC2/latest/APIR

eference/Welcome.html• SDKによってサポートする操作が異なる

APIコールに対する制限は、 Identity and Access Management(IAM) のドキュメントを参照

Page 17: Aws tools for power shellをつかいこなそう

AWS SDK

下記の言語/環境で提供中

Java Python PHP .NET Ruby

JavaScriptin nodeJS iOS AndroidJavaScript

in the Browser

Page 18: Aws tools for power shellをつかいこなそう

AWS SDK for .NET

AWS SDK for .NET� Amazon提供のAWS開発⽤.NET SDK� http://aws.amazon.com/sdkfornet/� 環境:• .NET Framework 3.5以降• Visual Studio 2010以降

� 最新版2.0.4� C#およびVisual Basicをサポート

Page 19: Aws tools for power shellをつかいこなそう

サンプル:EC2インスタンス起動// EC2操作⽤のクライアントvar ec2Client = new AmazonEC2Client();

// ⽴ち上げたいインスタンス情報の作成var runInstanceRequest = new RunInstancesRequest{

ImageId = "ami-xxx",

MaxCount = 1,

MinCount = 1,

KeyName = “YourKeyName",

SecurityGroups = { “yourSecurityGroup” },

InstanceType = InstanceType.M1Small

};

// インスタンスの起動ec2Client.RunInstances(runInstanceRequest);

Page 20: Aws tools for power shellをつかいこなそう

AWS SDK for .NETに含まれるもの

AWS Toolkit for Microsoft Visual Studio

Visual Studioプロジェクトテンプレート

AWS Tools for Windows PowerShell

AWS .NETライブラリ

C#コードサンプル

ドキュメント

Page 21: Aws tools for power shellをつかいこなそう

AWS Tools for Windows PowerShell

AWSへの容易なコマンドラインアクセス• 800以上のコマンドレット• AWS SDK for .NETがベース

スクリプトによる管理の自動化に対応EC2, S3をはじめ多くのサービスの管理が可能• http://aws.amazon.com/jp/powershell/

21

Page 22: Aws tools for power shellをつかいこなそう

インストール方法

以下のページの右上隅にある「AWS Tools for Windows」ボタンをクリック• http://aws.amazon.com/powershell/

ファイルを保存するかどうかをたずねるメッセージがブラウザに表示されたら、ローカルのディスクに保存保存したインストーラを開いてインストールプロセスを開始

Page 23: Aws tools for power shellをつかいこなそう

サポートしているサービス

Page 24: Aws tools for power shellをつかいこなそう

初期設定と動作確認

Windows PowerShell for AWSの起動• 「スタート」→「Amazon Web Services」→「Windows

PowerShell for AWS」

クレデンシャルとデフォルトリージョンの設定• PS C:\> Initialize-AWSDefaults

バージョンと利⽤可能なサービスの確認• PS C:\> Get-AWSPowerShellVersion -ListServices

24

Page 25: Aws tools for power shellをつかいこなそう

リージョンの設定

リージョン一覧の確認• PS C:> Get-AWSRegion

デフォルトリージョンの指定• PS C:> Set-DefaultAWSRegion ap-northeast-1

デフォルトリージョンの確認• PS C:> Get-DefaultAWSRegion

25

Page 26: Aws tools for power shellをつかいこなそう

EC2:Key Pairの作成

Key Pairの作成• PS C:\> $myPSKeyPair = New-EC2KeyPair –KeyName

myPSKeyPair

Private Keyのファイルへの書き出し• PS C:\> $myPSKeyPair.KeyMaterial | Out-File -Encoding

ascii myPSKeyPair.pem

26

Page 27: Aws tools for power shellをつかいこなそう

Key Pairとは?

Key Pairを作成すると、1組の公開鍵・秘密鍵が作成される公開鍵はAWS側で保管。EC2起動時にインスタンスにコピー秘密鍵はローカル側で保管。Windowsインスタンスの場合、パスワードの復号化に必要リージョンごとに独⽴

キーペアの作成

公開鍵は、AWSによってEC2起動時に埋め込まれる

秘密鍵は、デスクトップにダウンロードされる

EC2インスタンスは2つの鍵を用いる

Page 28: Aws tools for power shellをつかいこなそう

EC2:Security Groupの作成

Security Groupの作成• PS C:\> New-EC2SecurityGroup –GroupName

myPSSecurityGroup –GroupDescription “AWS Security Group from PowerShell”

Security Groupの確認• PS C:> Get-EC2SecurityGroup -GroupNames

myPSSecurityGroup

ポート番号の設定• PS C:\> Grant-EC2SecurityGroupIngress –GroupId sg-

3997fc38 –IpProtocol tcp –FromPort 3389 –toPort 3389 –CidrIp 0.0.0.0/0

28

Page 29: Aws tools for power shellをつかいこなそう

Security Groupとは?

インスタンスへのトラフィックを制限するSemi-statefulなファイアーウォール機能デフォルトでは全トラフィックが閉じており、外からアクセスできない必要な受信アクセスに対してアクセスルールを定義する• プロトコル( TCP/UDP)• 宛先ポート• アクセス元IP / Security Group

設定したルール以外の受信トラフィックは破棄されるルールをひとまとめにしたテンプレートを Security Groupと呼ぶ

EC2 インスタンス

ポート3389(RDP)

ポート80(HTTP)

EC2ファイアウォール

Security Group

ポート25(SMTP)

Page 30: Aws tools for power shellをつかいこなそう

EC2:AMIの取得

AMI一覧の取得• PS C:\> Get-EC2Image –owner amazon, self

AMI名前一覧の取得• PS C:> Get-EC2ImageByName

特定のAMI IDの取得• PS C:\> Get-EC2ImageByName –Names

WINDOWS_2012_BASE

30

Page 31: Aws tools for power shellをつかいこなそう

Amazon Machine Image (AMI)

AMIはインスタンス作成に使⽤するイメージテンプレートAWS公式AMIや、サードパーティもAMIを提供• https://aws.amazon.com/amis/• https://aws.amazon.com/marketplace/

選択したAMIから起動するEC2インスタンスから、任意のタイミングでカスタムAMIを作成できる

launch

AmazonMachineImage (AMI)

Amazon EC2インスタンス

Page 32: Aws tools for power shellをつかいこなそう

EC2:インスタンスの起動と開始

EC2インスタンスの起動• PS C:\> New-EC2Instance –ImageID ami-b1492eb0 –

MinCount 1 –MaxCount 1 –KeyName myPSKeyPair –SecurityGroup myPSSecurityGroup –InstanceType t1.micro

Windowsパスワードの取得• PS C:\> Get-EC2PasswordData –InstanceId i-7d5ccd78 –

PemFile myPSKeyPair.pem

EC2インスタンスの停止・開始• PS C:\> Stop-EC2Instance –InstanceIds i-7d5ccd78• PS C:\> Start-EC2Instance –InstanceIds i-7d5ccd78

32

Page 33: Aws tools for power shellをつかいこなそう

起動時にパラメーターを渡す起動時に16KB以内で任意のものを渡せる

ChefやPuppetなどを利⽤して、自⼰構成できるAMIに参照 AWSマイスター CloudFormation資料http://www.slideshare.net/kentamagawa/cloudformation-aws5

Page 34: Aws tools for power shellをつかいこなそう

EC2: User Dataの使用

User Dataにスクリプト(<script></script>)またはPowerShell(<powershell></powershell>)を埋め込むことで、インスタンス起動時の処理を自動化• <powershell>Add-WindowsFeature –Name Web-

Server</powershell>

User Dataはbase64でエンコードする必要がある

Windowsインスタンスが利⽤可能になるとすぐに実⾏される

Page 35: Aws tools for power shellをつかいこなそう

S3:S3の操作(1/2)

S3バケットの作成• PS C:\>New-S3Bucket –BucketName mypsbucket

オブジェクトのアップロード• PS C:\>Write-S3Object –BucketName mypsbucket –Key

sample.txt –File .\sample.txt

フォルダーのアップロード• PS C:\>Write-S3Object –BucketName mypsbucket –

Folder .\Scripts –KeyPrefix SampleScripts\

35

Page 36: Aws tools for power shellをつかいこなそう

S3:S3の操作(2/2)

オブジェクトの削除• PS C:\>Remove-S3Object –BucketName mypsbucket –

Key sample.txt

S3バケットの削除• PS C:\>Remove-S3Bucket –BucketName mypsbucket

36

Page 37: Aws tools for power shellをつかいこなそう

S3:スクリプトによる操作

例:C:\Logsにある1週間より前のファイルをS3のバケットにアップロードしてローカルから削除するスクリプト

foreach ($i in Get-ChildItem C:\Logs){

if ($i.CreationTime -lt ($(Get-Date).AddDays(-7))){

if ($i.Length -gt 0){

Write-S3Object -BucketName mylogbucket -Key Logs/$i -File $i.FullName

}Remove-Item $i.FullName

}}

37

Page 38: Aws tools for power shellをつかいこなそう

DynamoDB:テーブルの作成(1/2)

テーブルスキーマの作成• PS C:\> New-DDBTableSchema

キースキーマの追加• PS C:\> Add-DDBKeySchema -KeyName "ForumName" -KeyDataType

"S"• PS C:\> Add-DDBKeySchema -KeyName "Subject" -KeyType "range" -

KeyDataType "S“

インデックススキーマの追加• PS C:\> Add-DDBIndexSchema -IndexName "LastPostIndex" `

-RangeKeyName "LastPostDateTime" `-RangeKeyDataType "S" `-ProjectionType "keys_only"

テーブルの作成• New-DDBTable "Threads" -ReadCapacity 10 -WriteCapacity 5

38

Page 39: Aws tools for power shellをつかいこなそう

DynamoDB:テーブルの作成(2/2)

スキーマを指定したテーブルの作成

PS C:\> New-DDBTableSchema `| Add-DDBKeySchema -KeyName "ForumName" -

KeyDataType "S" `| Add-DDBKeySchema -KeyName "Subject" -KeyType

"range" -KeyDataType "S" `| Add-DDBIndexSchema -IndexName "LastPostIndex" `

-RangeKeyName "LastPostDateTime" `-RangeKeyDataType "S" `-ProjectionType "keys_only" `

| New-DDBTable "Threads" -ReadCapacity 10 -WriteCapacity 5

39

Page 40: Aws tools for power shellをつかいこなそう

まとめ

Amazon Web ServicesはWindowsを利⽤するためにも最適なクラウドサービス

AWS Tools for Windows PowerShellにより、PowerShellからAWSのさまざまなサービスを管理することが可能に

スクリプティングで管理の自動化も自由自在に!

40

Page 41: Aws tools for power shellをつかいこなそう

参考情報

Windows PowerShell• http://technet.microsoft.com/ja-jp/library/bb978526.aspx

Windows PowerShellでのスクリプティング• http://technet.microsoft.com/ja-

jp/scriptcenter/powershell.aspxAWS Tools for Windows PowerShell• http://aws.amazon.com/jp/powershell/

AWS Tools for Windows PowerShell User Guide• http://docs.aws.amazon.com/powershell/latest/userguide

/pstools-welcome.html

41