{"id":17254,"date":"2019-10-04T10:04:48","date_gmt":"2019-10-04T01:04:48","guid":{"rendered":"http:\/\/www.skyarch.net\/blog\/?p=17254"},"modified":"2019-10-04T10:44:59","modified_gmt":"2019-10-04T01:44:59","slug":"cdkpython%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6ec2%e3%82%a4%e3%83%b3%e3%82%b9%e3%82%bf%e3%83%b3%e3%82%b9%e3%82%92%e6%a7%8b%e7%af%89%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.skyarch.net\/blog\/cdkpython%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6ec2%e3%82%a4%e3%83%b3%e3%82%b9%e3%82%bf%e3%83%b3%e3%82%b9%e3%82%92%e6%a7%8b%e7%af%89%e3%81%99%e3%82%8b\/","title":{"rendered":"CDK(Python)\u3092\u4f7f\u3063\u3066EC2\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u69cb\u7bc9\u3059\u308b"},"content":{"rendered":"<p>\u6700\u8fd1CDK\u306b\u30cf\u30de\u3063\u3066\u3044\u307e\u3059\u3001\u30b7\u30b9\u30c6\u30e0\u30a2\u30fc\u30ad\u30c6\u30af\u30c8\u90e8 \u30b5\u30fc\u30d0\u30ec\u30b9\u958b\u767a\u8ab2 \u77f3\u9ed2\u3067\u3059\u3002<br \/>\nCDK\u3067\u7c21\u5358\u306bEC\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3059\u308b\u30c7\u30e2\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u7c21\u5358\u306a\u6d41\u308c\u3068\u3057\u3066\u306f<br \/>\n\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u4f5c\u6210\u2192\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u2192\u30b3\u30fc\u30c9\u66f8\u304f\u2192\u30c7\u30d7\u30ed\u30a4\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u305d\u308c\u3067\u306f\u3084\u3063\u3066\u884c\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u6210\u3057\u3066Python\u3067\u958b\u767a\u7528\u306b\u7a7a\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059<br \/>\n\u6700\u8fd1\u53e4\u3044CDK\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u4f7f\u3063\u3066\u3044\u308b\u3068\u3001\u65b0\u3057\u3044\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u3042\u308a\u307e\u3059\u3068\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u51fa\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<br \/>\n1.10\u4ee5\u524d\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3060\u3068\u30c7\u30d7\u30ed\u30a4\u6642\u306b\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>\n$ mkdir test-cdk-project  &amp;&amp; cd test-cdk-project\n$ cdk init --language python\nApplying project template app for python\nInitializing a new git repository...\nExecuting Creating virtualenv...\n\n# Welcome to your CDK Python project!\n\nThis is a blank project for Python development with CDK.\n\nThe `cdk.json` file tells the CDK Toolkit how to execute your app.\n...\n**************************************************\n*** Newer version of CDK is available [1.10.1] ***\n*** Upgrade recommended                        ***\n**************************************************\n<\/pre>\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u69cb\u9020\u306f\u4e3b\u306b\u3053\u3093\u306a\u611f\u3058\u306b\u306a\u3063\u3066\u3044\u307e\u3059(\u4e00\u90e8\u7701\u7565)<br \/>\n\u4e3b\u306b\u89e6\u308b\u306e\u306fapp.py\u3001setup.py\u3001test_cdk_project_stack.py\u306e3\u3064\u3060\u3051\u3067\u3059<\/p>\n<pre>\n.\n\u251c\u2500\u2500 .env\n|   \u2514\u2500\u2500...\n\u251c\u2500\u2500 README.md\n\u251c\u2500\u2500 app.py\n\u251c\u2500\u2500 cdk.json\n\u251c\u2500\u2500 requirements.txt\n\u251c\u2500\u2500 setup.py\n\u2514\u2500\u2500 test_cdk_project\n    \u251c\u2500\u2500 __init__.py\n    \u2514\u2500\u2500 test_cdk_project_stack.py\n<\/pre>\n<p>\u4eee\u60f3\u74b0\u5883\u3092\u6709\u52b9\u5316<\/p>\n<pre>\n$ source .env\/bin\/activate\n<\/pre>\n<p>\u4eca\u56deEC2\u3092\u4f5c\u6210\u3059\u308b\u306e\u306b\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u3092setup.py\u306b\u8ffd\u8a18<\/p>\n<pre>\n...\ninstall_requires=[\n        \"aws-cdk.core\",\n        \"aws_cdk.aws_ec2\",\n],\n...\n<\/pre>\n<p>pip\u3092\u30a2\u30c3\u30d7\u30c7\u30fc\u30c8\u3057\u3066\u3001\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n<pre>\n$ pip install --upgrade pip &amp;&amp; pip install -r requirements.txt\n<\/pre>\n<p>app.py\u306b\u30b9\u30bf\u30c3\u30af\u3092\u3069\u3053\u306e\u30ea\u30fc\u30b8\u30e7\u30f3\u306b\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u304b\u3092\u5b9a\u7fa9\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre>\n#!\/usr\/bin\/env python3\n\nfrom aws_cdk import core\n\nfrom test_cdk_project.test_cdk_project_stack import TestCdkProjectStack\n\n\napp = core.App()\nTestCdkProjectStack(app, \"test-cdk-project\", env={'region': 'ap-north-east-01'})\n\napp.synth()\n<\/pre>\n<p>\u6b21\u306btest_cdk_project_stack.py\u30d5\u30a1\u30a4\u30eb\u3092\u7de8\u96c6\u3057\u3066\u3044\u304d\u307e\u3059\u3002<br \/>\n\u3053\u3053\u3067EC2\u3092\u4f5c\u6210\u3059\u308b\u30b9\u30bf\u30c3\u30af\u3092\u69cb\u6210\u3057\u307e\u3059\u3002<br \/>\nVPC\u3001SecurityGroup\u3082\u4f5c\u6210\u3055\u305b\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre>\nfrom aws_cdk import (\n    aws_ec2,\n    core,\n)\n\n\nclass TestCdkProjectStack(core.Stack):\n\n    def __init__(self, scope: core.Construct, id: str, **kwargs) -&gt; None:\n        super().__init__(scope, id, **kwargs)\n\n        cidr = '10.0.0.0\/16'\n\n        vpc = aws_ec2.Vpc(\n            self,\n            id='test-vpc',\n            cidr=cidr,\n            nat_gateways=1,\n            subnet_configuration=[\n                aws_ec2.SubnetConfiguration(\n                    cidr_mask=18,\n                    name='public',\n                    subnet_type=aws_ec2.SubnetType.PUBLIC,\n                ),\n                aws_ec2.SubnetConfiguration(\n                    cidr_mask=18,\n                    name='private',\n                    subnet_type=aws_ec2.SubnetType.PRIVATE,\n                ),\n            ],\n        )\n\n        security_group = aws_ec2.SecurityGroup(\n            self,\n            id='test-security-group',\n            vpc=vpc,\n            security_group_name='test-security-group'\n        )\n\n        security_group.add_ingress_rule(\n            peer=aws_ec2.Peer.ipv4(cidr),\n            connection=aws_ec2.Port.tcp(22),\n        )\n\n        image_id = aws_ec2.AmazonLinuxImage(generation=aws_ec2.AmazonLinuxGeneration.AMAZON_LINUX_2).get_image(self).image_id\n\n        aws_ec2.CfnInstance(\n            self,\n            id='test-instance',\n            availability_zone=\"ap-northeast-1a\",\n            image_id=image_id,\n            instance_type=\"t2.micro\",\n            key_name='test-ssh-key',\n            security_group_ids=[security_group.security_group_id],\n            subnet_id=vpc.private_subnets[0].subnet_id,\n            tags=[{\n                \"key\": \"Name\",\n                \"value\": \"test-instance\"\n            }]\n        )\n<\/pre>\n<p>\u3053\u3053\u304b\u3089\u306f\u4f5c\u6210\u3057\u305f\u30b9\u30bf\u30c3\u30af\u3092\u30c7\u30d7\u30ed\u30a4\u3057\u3066\u6d41\u308c\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u306fCDK\u306e\u30c4\u30fc\u30eb\u30ad\u30c3\u30c8\u30b9\u30bf\u30c3\u30af\u3092\u30c7\u30d7\u30ed\u30a4\u3057\u307e\u3059\u3002<br \/>\n\u3053\u308c\u306f\u521d\u3081\u3066\u305d\u306e\u30ea\u30fc\u30b8\u30e7\u30f3\u306b\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u66421\u56de\u3060\u3051\u3067\u5927\u4e08\u592b\u3067\u3059\u3002<\/p>\n<pre>\n$ cdk bootstrap \n<\/pre>\n<p>\u6b21\u306b\u3053\u306e\u30b3\u30de\u30f3\u30c9\u3092\u8a66\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br \/>\n\u30b9\u30bf\u30c3\u30af\u306eCFn\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u304c\u6a19\u6e96\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre>\n$ cdk synth\nResources:\n  testvpc8985080E:\n    Type: AWS::EC2::VPC\n    Properties:\n      CidrBlock: 10.0.0.0\/16\n      EnableDnsHostnames: true\n      EnableDnsSupport: true\n      InstanceTenancy: default\n...\n<\/pre>\n<p>\u3053\u306e\u6642\u3001cdk.out\u3068\u3044\u3046\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u304c\u751f\u6210\u3055\u308c\u3001\u305d\u306e\u4e2d\u306etest-cdk-project.template.json\u306b\u3082\u51fa\u529b\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u30c7\u30d7\u30ed\u30a4\u524d\u306e\u69cb\u6210\u78ba\u8a8d\u7528\u9014\u3067\u3059<\/p>\n<pre>\n.\n\u251c\u2500\u2500 app.py\n\u251c\u2500\u2500 cdk.json\n\u251c\u2500\u2500 cdk.out\n\u2502   \u251c\u2500\u2500 cdk.out\n\u2502   \u251c\u2500\u2500 manifest.json\n\u2502   \u251c\u2500\u2500 test-cdk-project.template.json\n\u2502   \u2514\u2500\u2500 tree.json\n<\/pre>\n<p>\u30c7\u30d7\u30ed\u30a4\u53ef\u80fd\u306a\u30b9\u30bf\u30c3\u30af\u3092\u4e00\u89a7\u8868\u793a\u3057\u3066\u3001\u6307\u5b9a\u306e\u30b9\u30bf\u30c3\u30af\u3092\u30c7\u30d7\u30ed\u30a4\u958b\u59cb\u3067\u3059\u3002<\/p>\n<pre>\n$ cdk ls\ntest-cdk-project\n$ cdk deploy test-cdk-project\ntest-cdk-project: deploying...\ntest-cdk-project: creating CloudFormation changeset...\n<\/pre>\n<p>\u30c7\u30d7\u30ed\u30a4\u304c\u5b8c\u4e86\u3057\u305f\u3089\u30b3\u30f3\u30bd\u30fc\u30eb\u3067\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3057\u3087\u3046<br \/>\nCloudFormation\u306e\u30b9\u30bf\u30c3\u30af\u306b\u30c7\u30d7\u30ed\u30a4\u3057\u305f\u30b9\u30bf\u30c3\u30af\u304c\u8868\u793a\u3055\u308c\u3066\u3044\u307e\u3059<br \/>\n<a href=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/9a10a11c8a764d194d7d724b87bebf1a.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/9a10a11c8a764d194d7d724b87bebf1a.png\" alt=\"\" width=\"1464\" height=\"603\" class=\"aligncenter size-full wp-image-17259\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/9a10a11c8a764d194d7d724b87bebf1a.png 1464w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/9a10a11c8a764d194d7d724b87bebf1a-300x124.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/9a10a11c8a764d194d7d724b87bebf1a-768x316.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/9a10a11c8a764d194d7d724b87bebf1a-1024x422.png 1024w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/9a10a11c8a764d194d7d724b87bebf1a-728x300.png 728w\" sizes=\"auto, (max-width: 1464px) 100vw, 1464px\" \/><\/a><\/p>\n<p>\u4f5c\u6210\u3057\u305f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3082\u304d\u3061\u3093\u3068\u4f5c\u3089\u308c\u3066\u3044\u307e\u3059\u306d<br \/>\n\u304a\u75b2\u308c\u69d8\u3067\u3057\u305f\u3002<br \/>\n<a href=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/2200f31ad2cf1ab28443398b53972804.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/2200f31ad2cf1ab28443398b53972804.png\" alt=\"\" width=\"1587\" height=\"819\" class=\"aligncenter size-full wp-image-17260\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/2200f31ad2cf1ab28443398b53972804.png 1587w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/2200f31ad2cf1ab28443398b53972804-300x155.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/2200f31ad2cf1ab28443398b53972804-768x396.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/2200f31ad2cf1ab28443398b53972804-1024x528.png 1024w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2019\/10\/2200f31ad2cf1ab28443398b53972804-728x376.png 728w\" sizes=\"auto, (max-width: 1587px) 100vw, 1587px\" \/><\/a><\/p>\n<h1>\u307e\u3068\u3081<\/h1>\n<p>CDK\u3092\u4f7f\u3046\u3068\u7c21\u5358\u306bAWS\u306e\u74b0\u5883\u3092\u69cb\u7bc9\u3067\u304d\u3066\u3057\u307e\u3044\u307e\u3059\u3002<br \/>\n\u307e\u3055\u306bInfrastructure as Code\u3067\u3059\u306d\u3002<br \/>\ncdk docs\u30b3\u30de\u30f3\u30c9\u3067API\u306e\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u306e\u30ea\u30f3\u30af\u306b\u98db\u3076\u306e\u3067<br \/>\n\u8272\u3005\u306a\u69cb\u6210\u3067\u305c\u3072\u8a66\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<br \/>\n\u3061\u306a\u307f\u306bAPI\u306e\u63a5\u982d\u306bCfn\u3068\u540d\u306e\u3064\u304f\u3082\u306e\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u3053\u308c\u306f\u9ad8\u30ec\u30d9\u30ebAPI\u304b\u4f4e\u30ec\u30d9\u30ebAPI\u304b\u306e\u9055\u3044\u3067\u3059\u3002<br \/>\n\u9ad8\u30ec\u30d9\u30ebAPI\u306e\u65b9\u304c\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067<br \/>\n\u57fa\u672c\u9ad8\u30ec\u30d9\u30ebAPI\u3092\u4f7f\u7528\u3059\u308b\u65b9\u304c\u826f\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u305f\u3060\u9ad8\u30ec\u30d9\u30ebAPI\u3060\u3068\u63d0\u4f9b\u3055\u308c\u3066\u3044\u306a\u3044\u6a5f\u80fd(RDS\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30b0\u30eb\u30fc\u30d7\u6307\u5b9a\u306a\u3069)\u304c\u3042\u308b\u306e\u3067\u3001<br \/>\n\u81ea\u5206\u304c\u8a2d\u5b9a\u3057\u305f\u3044\u5c5e\u6027\u304c\u306a\u3044\u5834\u5408\u306b\u306f\u3001\u63a5\u982d\u306bCfn\u304c\u3064\u304f\u4f4e\u30ec\u30d9\u30ebAPI\u3092\u63a2\u3059\u3068\u826f\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>CDK\u306fAWS\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u30b3\u30fc\u30c9\u3067\u66f8\u3044\u3066\u8a2d\u5b9a\u3059\u308b\u308f\u3051\u306a\u306e\u3067\u3001AWS\u306e\u30b5\u30fc\u30d3\u30b9\u306b\u5bfe\u3059\u308b\u77e5\u8b58\u304c\u990a\u308f\u308c\u307e\u3059\u3002<br \/>\n\u6700\u8fd1SAA\u3092\u53d6\u5f97\u3057\u305f\u306e\u3067\u3059\u304c\u3001CDK\u3067\u66f8\u3044\u305f\u3053\u3068\u304c\u6df1\u3044\u7406\u89e3\u306b\u5f79\u306b\u7acb\u3063\u305f\u6c17\u304c\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\nCDK\u6700\u9ad8...!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1CDK\u306b\u30cf\u30de\u3063\u3066\u3044\u307e\u3059\u3001\u30b7\u30b9\u30c6\u30e0\u30a2\u30fc\u30ad\u30c6\u30af\u30c8\u90e8 \u30b5\u30fc\u30d0\u30ec\u30b9\u958b\u767a\u8ab2 \u77f3\u9ed2\u3067\u3059\u3002 CDK\u3067\u7c21\u5358\u306bEC\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3059\u308b\u30c7\u30e2\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u7c21\u5358\u306a\u6d41\u308c\u3068\u3057\u3066\u306f \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u4f5c\u6210\u2192\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u2192&#8230;<\/p>\n","protected":false},"author":1,"featured_media":17262,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"17254","footnotes":""},"categories":[20,829,270],"tags":[706,542,65],"class_list":{"0":"post-17254","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-aws","8":"category-cdk","9":"category-infrastructure-as-code","10":"tag-aws-cdk","11":"tag-aws-cloudformation","12":"tag-python","13":"ja"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/17254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/comments?post=17254"}],"version-history":[{"count":11,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/17254\/revisions"}],"predecessor-version":[{"id":17285,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/17254\/revisions\/17285"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media\/17262"}],"wp:attachment":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media?parent=17254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/categories?post=17254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/tags?post=17254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}