example.cft 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. # Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. AWSTemplateFormatVersion: '2010-09-09'
  3. Description: strongSwan VPN Gateway as an EC2 Instance
  4. Metadata:
  5. AWS::CloudFormation::Interface:
  6. ParameterGroups:
  7. - Label:
  8. default: System Classification
  9. Parameters:
  10. - pOrg
  11. - pSystem
  12. - pApp
  13. - Label:
  14. default: System Environment
  15. Parameters:
  16. - pEnvPurpose
  17. - Label:
  18. default: VPN Tunnel 1
  19. Parameters:
  20. - pTunnel1Psk
  21. - pTunnel1VgwOutsideIpAddress
  22. - pTunnel1VgwInsideCidr
  23. - pTunnel1CgwInsideCidr
  24. - pTunnel1BgpAsn
  25. - pTunnel1BgpNeighborIpAddress
  26. - Label:
  27. default: VPN Tunnel 2
  28. Parameters:
  29. - pTunnel2Psk
  30. - pTunnel2VgwOutsideIpAddress
  31. - pTunnel2VgwInsideCidr
  32. - pTunnel2CgwInsideCidr
  33. - pTunnel2BgpAsn
  34. - pTunnel2BgpNeighborIpAddress
  35. - Label:
  36. default: Local Network Configuration
  37. Parameters:
  38. - pVpcId
  39. - pVpcCidr
  40. - pSubnetId
  41. - pUseElasticIp
  42. - pEipAllocationId
  43. - pLocalBgpAsn
  44. - Label:
  45. default: EC2
  46. Parameters:
  47. - pAmiId
  48. - pInstanceType
  49. ParameterLabels:
  50. pOrg:
  51. default: Organization Identifier
  52. pSystem:
  53. default: System Identifier
  54. pApp:
  55. default: Application Identifier
  56. pEnvPurpose:
  57. default: Environment Purpose
  58. pTunnel1Psk:
  59. default: VPN Tunnel 1 Pre-Shared Key
  60. pTunnel1VgwOutsideIpAddress:
  61. default: VPN Tunnel 1 Virtual Private Gateway Outside IP Address
  62. pTunnel1VgwInsideCidr:
  63. default: VPN Tunnel 1 Virtual Private Gateway Inside CIDR
  64. pTunnel1CgwInsideCidr:
  65. default: VPN Tunnel 1 Customer Gateway Inside CIDR
  66. pTunnel1BgpAsn:
  67. default: VPN Tunnel 1 BGP ASN
  68. pTunnel1BgpNeighborIpAddress:
  69. default: VPN Tunnel 1 BGP Neighbor IP Address
  70. pTunnel2Psk:
  71. default: VPN Tunnel 2 Pre-Shared Key
  72. pTunnel2VgwOutsideIpAddress:
  73. default: VPN Tunnel 2 Virtual Private Gateway Outside IP Address
  74. pTunnel2VgwInsideCidr:
  75. default: VPN Tunnel 2 Virtual Private Gateway Inside CIDR
  76. pTunnel2CgwInsideCidr:
  77. default: VPN Tunnel 2 Customer Gateway Inside CIDR
  78. pTunnel2BgpAsn:
  79. default: VPN Tunnel 2 BGP ASN
  80. pTunnel2BgpNeighborIpAddress:
  81. default: VPN Tunnel 2 BGP Neighbor IP Address
  82. pUseElasticIp:
  83. default: Use Elastic IP Address? (true/false)
  84. pEipAllocationId:
  85. default: Elastic IP Address Allocation ID
  86. pLocalBgpAsn:
  87. default: Local VPN Gateway's BGP ASN
  88. pVpcId:
  89. default: VPC ID
  90. pVpcCidr:
  91. default: VPC CIDR Block
  92. pSubnetId:
  93. default: Subnet ID for VPN Gateway
  94. pInstanceType:
  95. default: EC2 Instance Type
  96. pAmiId:
  97. default: EC2 AMI ID
  98. Parameters:
  99. pOrg:
  100. Type: String
  101. Description: Used to qualify resource names
  102. Default: example
  103. pSystem:
  104. Type: String
  105. Description: Used to qualify resource names
  106. Default: infra
  107. pApp:
  108. Type: String
  109. Description: Used to qualify resource names
  110. Default: vpngw
  111. pEnvPurpose:
  112. Type: String
  113. Description: Used to qualify resource names. 10 characters max.
  114. AllowedPattern: '^[a-zA-Z0-9-_]{1,10}$'
  115. pTunnel1Psk:
  116. Description: VPN Tunnel 1 Pre-Shared Key
  117. Type: String
  118. pTunnel1VgwOutsideIpAddress:
  119. Description: VPN Tunnel 1 Virtual Private Gateway Outside IP Address
  120. Type: String
  121. pTunnel1VgwInsideCidr:
  122. Description: VPN Tunnel 1 Virtual Private Gateway Inside CIDR
  123. Type: String
  124. AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(1[6-9]|3[0-8]))$
  125. pTunnel1CgwInsideCidr:
  126. Description: VPN Tunnel 1 Customer Gateway Inside CIDR
  127. Type: String
  128. AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(1[6-9]|3[0-8]))$
  129. pTunnel1BgpAsn:
  130. Description: VPN Tunnel 1 BGP ASN
  131. Type: Number
  132. Default: 64512
  133. pTunnel1BgpNeighborIpAddress:
  134. Description: VPN Tunnel 1 BGP Neighbor IP Address
  135. Type: String
  136. pTunnel2Psk:
  137. Description: VPN Tunnel 2 Pre-Shared Key
  138. Type: String
  139. pTunnel2VgwOutsideIpAddress:
  140. Description: VPN Tunnel 2 Virtual Private Gateway Outside IP Address
  141. Type: String
  142. pTunnel2VgwInsideCidr:
  143. Description: VPN Tunnel 2 Virtual Private Gateway Inside CIDR
  144. Type: String
  145. AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(1[6-9]|3[0-8]))$
  146. pTunnel2CgwInsideCidr:
  147. Description: VPN Tunnel 2 Customer Gateway Inside CIDR
  148. Type: String
  149. AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(1[6-9]|3[0-8]))$
  150. pTunnel2BgpAsn:
  151. Description: VPN Tunnel 2 BGP ASN
  152. Type: Number
  153. Default: 64512
  154. pUseElasticIp:
  155. Type: String
  156. Description: Whether Elastic IP address is to be used.
  157. Default: false
  158. AllowedValues: [true, false]
  159. pEipAllocationId:
  160. Description: Elastic IP Address Alocation ID
  161. Type: String
  162. pLocalBgpAsn:
  163. Description: Local VPN Gateway's BGP ASN
  164. Type: Number
  165. Default: 65000
  166. pTunnel2BgpNeighborIpAddress:
  167. Description: VPN Tunnel 2 BGP Neighbor IP Address
  168. Type: String
  169. pVpcId:
  170. Description: VPC ID
  171. Type: AWS::EC2::VPC::Id
  172. pVpcCidr:
  173. Description: VPC CIDR Block
  174. Type: String
  175. AllowedPattern: ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(1[6-9]|2[0-8]))$
  176. pSubnetId:
  177. Description: Subnet ID for VPN Gateway
  178. Type: AWS::EC2::Subnet::Id
  179. pInstanceType:
  180. Description: EC2 Instance Type
  181. Type: String
  182. Default: t3a.micro
  183. AllowedValues:
  184. - t3a.micro
  185. - t3a.small
  186. - t3a.medium
  187. ConstraintDescription: must be a valid EC2 instance type.
  188. pAmiId:
  189. Description: EC2 AMI ID
  190. Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
  191. Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-ebs'
  192. Rules:
  193. SubnetsInVPC:
  194. Assertions:
  195. - Assert:
  196. 'Fn::EachMemberIn':
  197. - 'Fn::ValueOfAll':
  198. - 'AWS::EC2::Subnet::Id'
  199. - VpcId
  200. - 'Fn::RefAll': 'AWS::EC2::VPC::Id'
  201. AssertDescription: All subnets must in the VPC
  202. Conditions:
  203. cUseElasticIp: !Equals [ !Ref 'pUseElasticIp', true ]
  204. Resources:
  205. rInstanceSecurityGroup:
  206. Type: AWS::EC2::SecurityGroup
  207. Properties:
  208. GroupName: !Sub '${pSystem}-${pApp}-ec2-${pEnvPurpose}'
  209. VpcId: !Ref pVpcId
  210. GroupDescription: Allow traffic from other VPN gateway and all locally sourced traffic
  211. SecurityGroupIngress:
  212. - IpProtocol: udp
  213. FromPort: 500
  214. ToPort: 500
  215. CidrIp: !Sub '${pTunnel1VgwOutsideIpAddress}/32'
  216. - IpProtocol: udp
  217. FromPort: 500
  218. ToPort: 500
  219. CidrIp: !Sub '${pTunnel2VgwOutsideIpAddress}/32'
  220. - IpProtocol: udp
  221. FromPort: 4500
  222. ToPort: 4500
  223. CidrIp: !Sub '${pTunnel1VgwOutsideIpAddress}/32'
  224. - IpProtocol: udp
  225. FromPort: 4500
  226. ToPort: 4500
  227. CidrIp: !Sub '${pTunnel2VgwOutsideIpAddress}/32'
  228. - IpProtocol: '50'
  229. CidrIp: !Sub '${pTunnel1VgwOutsideIpAddress}/32'
  230. - IpProtocol: '50'
  231. CidrIp: !Sub '${pTunnel2VgwOutsideIpAddress}/32'
  232. - IpProtocol: '51'
  233. CidrIp: !Sub '${pTunnel1VgwOutsideIpAddress}/32'
  234. - IpProtocol: '51'
  235. CidrIp: !Sub '${pTunnel2VgwOutsideIpAddress}/32'
  236. - IpProtocol: '-1'
  237. FromPort: 0
  238. ToPort: 65535
  239. CidrIp: !Ref pVpcCidr
  240. rLaunchTemplate:
  241. Type: AWS::EC2::LaunchTemplate
  242. Properties:
  243. LaunchTemplateName: !Sub '${pSystem}-${pApp}-${pEnvPurpose}'
  244. LaunchTemplateData:
  245. InstanceType: !Ref pInstanceType
  246. ImageId: !Ref pAmiId
  247. IamInstanceProfile:
  248. Arn: !GetAtt rInstanceProfile.Arn
  249. NetworkInterfaces:
  250. - DeviceIndex: 0
  251. DeleteOnTermination: true
  252. Description: !Sub '${pSystem}-${pApp}-${pEnvPurpose}'
  253. Groups:
  254. - !Ref rInstanceSecurityGroup
  255. AssociatePublicIpAddress: !Ref pUseElasticIp
  256. UserData:
  257. Fn::Base64: !Sub |
  258. #!/bin/bash -xe
  259. yum install -y aws-cfn-bootstrap
  260. /opt/aws/bin/cfn-init -v \
  261. --stack ${AWS::StackName} \
  262. --resource rLaunchTemplate \
  263. --configsets All \
  264. --region ${AWS::Region}
  265. /opt/aws/bin/cfn-signal -e $? \
  266. '${rVpnGatewayWaitHandle}'
  267. Metadata:
  268. AWS::CloudFormation::Init:
  269. configSets:
  270. All:
  271. - 01-ConfigureCloudWatchMetrics
  272. - 02-ConfigureCloudWatchLogsAgent
  273. - 03-InstallEpel
  274. - 04-ConfigureVpnGateway
  275. 01-ConfigureCloudWatchMetrics:
  276. packages:
  277. yum:
  278. perl-Switch: []
  279. perl-DateTime: []
  280. perl-Sys-Syslog: []
  281. perl-LWP-Protocol-https: []
  282. perl-Digest-SHA.x86_64: []
  283. sources:
  284. /home/ec2-user: >-
  285. https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip
  286. files:
  287. /home/ec2-user/crontab:
  288. content: !Sub |
  289. */1 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl --aws-iam-role=${rRole} --mem-used --memory-units=megabytes --mem-util --disk-space-util --disk-space-used --disk-space-avail --disk-path=/
  290. mode: '000600'
  291. owner: ec2-user
  292. group: ec2-user
  293. commands:
  294. 01-yum-update:
  295. command: yum update -y
  296. 02-monitoring-cron:
  297. command: >-
  298. chmod +x /home/ec2-user/aws-scripts-mon/*.pl && crontab -u
  299. ec2-user /home/ec2-user/crontab && rm /home/ec2-user/crontab
  300. 02-ConfigureCloudWatchLogsAgent:
  301. packages:
  302. yum:
  303. awslogs: []
  304. files:
  305. /etc/awslogs/awslogs.conf:
  306. content: !Sub |
  307. [general]
  308. state_file= /var/awslogs/state/agent-state
  309. [/var/log/cloud-init.log]
  310. file = /var/log/cloud-init.log
  311. log_group_name = ${rCloudWatchLogsAgentGroup}
  312. log_stream_name = {instance_id}/cloud-init.log
  313. datetime_format =
  314. [/var/log/cloud-init-output.log]
  315. file = /var/log/cloud-init-output.log
  316. log_group_name = ${rCloudWatchLogsAgentGroup}
  317. log_stream_name = {instance_id}/cloud-init-output.log
  318. datetime_format =
  319. [/var/log/cfn-init.log]
  320. file = /var/log/cfn-init.log
  321. log_group_name = ${rCloudWatchLogsAgentGroup}
  322. log_stream_name = {instance_id}/cfn-init.log
  323. datetime_format =
  324. [/var/log/cfn-wire.log]
  325. file = /var/log/cfn-wire.log
  326. log_group_name = ${rCloudWatchLogsAgentGroup}
  327. log_stream_name = {instance_id}/cfn-wire.log
  328. datetime_format =
  329. [/var/log/charon.log]
  330. file = /var/log/charon.log
  331. log_group_name = ${rCloudWatchLogsAgentGroup}
  332. log_stream_name = {instance_id}/charon.log
  333. datetime_format =
  334. [/var/log/quagga/zebra.log]
  335. file = /var/log/quagga/zebra.log
  336. log_group_name = ${rCloudWatchLogsAgentGroup}
  337. log_stream_name = {instance_id}/zebra.log
  338. datetime_format =
  339. [/var/log/quagga/bgpd.log]
  340. file = /var/log/quagga/bgpd.log
  341. log_group_name = ${rCloudWatchLogsAgentGroup}
  342. log_stream_name = {instance_id}/bgpd.log
  343. datetime_format =
  344. mode: '000444'
  345. owner: root
  346. group: root
  347. /etc/awslogs/awscli.conf:
  348. content: !Sub |
  349. [plugins]
  350. cwlogs = cwlogs
  351. [default]
  352. region = ${AWS::Region}
  353. mode: '000444'
  354. owner: root
  355. group: root
  356. commands:
  357. 01-create-awslogs-state-file:
  358. command: mkdir -p /var/awslogs/state
  359. 02-enable-awslogsd:
  360. command: systemctl enable awslogsd.service
  361. 03-start-awslogsd:
  362. command: systemctl start awslogsd
  363. 03-InstallEpel:
  364. commands:
  365. 01-install-epel:
  366. command: amazon-linux-extras install epel -y
  367. 04-ConfigureVpnGateway:
  368. packages:
  369. yum:
  370. strongswan: []
  371. ntp: []
  372. quagga: []
  373. files:
  374. /etc/strongswan/strongswan.conf:
  375. content: |
  376. # strongswan.conf - strongSwan configuration file
  377. #
  378. # Refer to the strongswan.conf(5) manpage for details
  379. #
  380. # Configuration changes should be made in the included files
  381. charon {
  382. plugins {
  383. include strongswan.d/charon/*.conf
  384. }
  385. load_modular = yes
  386. filelog {
  387. charon {
  388. path = /var/log/charon.log
  389. time_format = %b %e %T
  390. ike_name = yes
  391. append = yes
  392. }
  393. }
  394. }
  395. mode: '000600'
  396. owner: root
  397. group: root
  398. /etc/strongswan/ipsec.conf:
  399. content: !Sub |
  400. conn %default
  401. leftauth=psk
  402. rightauth=psk
  403. ike=aes256-sha256-modp2048s256,aes128-sha1-modp1024!
  404. ikelifetime=28800s
  405. aggressive=no
  406. esp=aes128-sha256-modp2048s256,aes128-sha1-modp1024!
  407. lifetime=3600s
  408. type=tunnel
  409. dpddelay=10s
  410. dpdtimeout=30s
  411. keyexchange=ikev1
  412. rekey=yes
  413. reauth=no
  414. dpdaction=restart
  415. closeaction=restart
  416. left=%defaultroute
  417. leftsubnet=0.0.0.0/0,::/0
  418. rightsubnet=0.0.0.0/0,::/0
  419. leftupdown=/etc/strongswan/ipsec-vti.sh
  420. installpolicy=yes
  421. compress=no
  422. mobike=no
  423. conn AWS-VPC-TUNNEL-1
  424. left=%any
  425. right=${pTunnel1VgwOutsideIpAddress}
  426. auto=start
  427. mark=100
  428. conn AWS-VPC-TUNNEL-2
  429. left=%any
  430. right=${pTunnel2VgwOutsideIpAddress}
  431. auto=start
  432. mark=200
  433. mode: '000600'
  434. owner: root
  435. group: root
  436. /etc/strongswan/ipsec-vti.sh:
  437. content: !Sub |
  438. #!/bin/bash
  439. #@ /etc/strongswan/ipsec-vti.sh (Centos) or /etc/strongswan.d/ipsec-vti.sh (Ubuntu)
  440. # AWS VPC Hardware VPN Strongswan updown Script
  441. # Usage Instructions:
  442. # Add "install_routes = no" to /etc/strongswan/strongswan.d/charon.conf or /etc/strongswan.d/charon.conf
  443. # Add "install_virtual_ip = no" to /etc/strongswan/strongswan.d/charon.conf or /etc/strongswan.d/charon.conf
  444. # For Ubuntu: Add "leftupdown=/etc/strongswan.d/ipsec-vti.sh" to /etc/ipsec.conf
  445. # For RHEL/Centos: Add "leftupdown=/etc/strongswan/ipsec-vti.sh" to /etc/strongswan/ipsec.conf
  446. # For RHEL/Centos 6 and below: git clone git://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git && cd iproute2 && make && cp ./ip/ip /usr/local/sbin/ip
  447. # Adjust the below according to the Generic Gateway Configuration file provided to you by AWS.
  448. # Sample: http://docs.aws.amazon.com/AmazonVPC/latest/NetworkAdminGuide/GenericConfig.html
  449. IP=$(which ip)
  450. IPTABLES=$(which iptables)
  451. PLUTO_MARK_OUT_ARR=(${!PLUTO_MARK_OUT//// })
  452. PLUTO_MARK_IN_ARR=(${!PLUTO_MARK_IN//// })
  453. case "$PLUTO_CONNECTION" in
  454. AWS-VPC-TUNNEL-1)
  455. VTI_INTERFACE=vti1
  456. VTI_LOCALADDR=${pTunnel1CgwInsideCidr}
  457. VTI_REMOTEADDR=${pTunnel1VgwInsideCidr}
  458. ;;
  459. AWS-VPC-TUNNEL-2)
  460. VTI_INTERFACE=vti2
  461. VTI_LOCALADDR=${pTunnel2CgwInsideCidr}
  462. VTI_REMOTEADDR=${pTunnel2VgwInsideCidr}
  463. ;;
  464. esac
  465. case "${!PLUTO_VERB}" in
  466. up-client)
  467. #$IP tunnel add ${!VTI_INTERFACE} mode vti local ${!PLUTO_ME} remote ${!PLUTO_PEER} okey ${!PLUTO_MARK_OUT_ARR[0]} ikey ${!PLUTO_MARK_IN_ARR[0]}
  468. $IP link add ${!VTI_INTERFACE} type vti local ${!PLUTO_ME} remote ${!PLUTO_PEER} okey ${!PLUTO_MARK_OUT_ARR[0]} ikey ${!PLUTO_MARK_IN_ARR[0]}
  469. sysctl -w net.ipv4.conf.${!VTI_INTERFACE}.disable_policy=1
  470. sysctl -w net.ipv4.conf.${!VTI_INTERFACE}.rp_filter=2 || sysctl -w net.ipv4.conf.${!VTI_INTERFACE}.rp_filter=0
  471. $IP addr add ${!VTI_LOCALADDR} remote ${!VTI_REMOTEADDR} dev ${!VTI_INTERFACE}
  472. $IP link set ${!VTI_INTERFACE} up mtu 1436
  473. $IPTABLES -t mangle -I FORWARD -o ${!VTI_INTERFACE} -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
  474. $IPTABLES -t mangle -I INPUT -p esp -s ${!PLUTO_PEER} -d ${!PLUTO_ME} -j MARK --set-xmark ${!PLUTO_MARK_IN}
  475. $IP route flush table 220
  476. #/etc/init.d/bgpd reload || /etc/init.d/quagga force-reload bgpd
  477. ;;
  478. down-client)
  479. #$IP tunnel del ${!VTI_INTERFACE}
  480. $IP link del ${!VTI_INTERFACE}
  481. $IPTABLES -t mangle -D FORWARD -o ${!VTI_INTERFACE} -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
  482. $IPTABLES -t mangle -D INPUT -p esp -s ${!PLUTO_PEER} -d ${!PLUTO_ME} -j MARK --set-xmark ${!PLUTO_MARK_IN}
  483. ;;
  484. esac
  485. mode: '000700'
  486. owner: root
  487. group: root
  488. /etc/strongswan/ipsec.secrets:
  489. content: !Sub |
  490. ${pTunnel1VgwOutsideIpAddress} : PSK "${pTunnel1Psk}"
  491. ${pTunnel2VgwOutsideIpAddress} : PSK "${pTunnel2Psk}"
  492. mode: '000600'
  493. owner: root
  494. group: root
  495. /etc/quagga/zebra.conf:
  496. content: |
  497. hostname {HOSTNAME}
  498. password zebra
  499. enable password zebra
  500. !
  501. log file /var/log/quagga/zebra.log
  502. !
  503. ! Configure interfaces
  504. interface lo
  505. ! Change preferred source ip address of received routes
  506. route-map RM_SET_SRC permit 10
  507. set src {PRIVATE_IP}
  508. ip protocol bgp route-map RM_SET_SRC
  509. !
  510. line vty
  511. mode: '000600'
  512. owner: quagga
  513. group: quagga
  514. /etc/quagga/bgpd.conf:
  515. content: !Sub |
  516. hostname bgpd
  517. password zebra
  518. enable password zebra
  519. !
  520. log file /var/log/quagga/bgpd.log
  521. !
  522. debug bgp events
  523. debug bgp filters
  524. debug bgp fsm
  525. debug bgp keepalives
  526. debug bgp updates
  527. !
  528. router bgp ${pLocalBgpAsn}
  529. bgp router-id {PRIVATE_IP}
  530. network ${pVpcCidr}
  531. neighbor ${pTunnel1BgpNeighborIpAddress} remote-as ${pTunnel1BgpAsn}
  532. neighbor ${pTunnel2BgpNeighborIpAddress} remote-as ${pTunnel2BgpAsn}
  533. neighbor ${pTunnel2BgpNeighborIpAddress} route-map RM_LOWER_PRIORITY out
  534. !
  535. route-map RM_LOWER_PRIORITY permit 10
  536. set as-path prepend ${pLocalBgpAsn} ${pLocalBgpAsn} ${pLocalBgpAsn}
  537. !
  538. line vty
  539. mode: '000600'
  540. owner: quagga
  541. group: quagga
  542. /etc/sysctl.conf:
  543. content: |
  544. # sysctl settings are defined through files in
  545. # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
  546. #
  547. # Vendors settings live in /usr/lib/sysctl.d/.
  548. # To override a whole file, create a new file with the same in
  549. # /etc/sysctl.d/ and put new settings there. To override
  550. # only specific settings, add a file with a lexically later
  551. # name in /etc/sysctl.d/ and put new settings there.
  552. #
  553. # For more information, see sysctl.conf(5) and sysctl.d(5).
  554. net.ipv4.ip_forward = 1
  555. net.ipv4.conf.all.send_redirects = 0
  556. net.ipv4.conf.default.send_redirects = 0
  557. net.ipv4.tcp_max_syn_backlog = 1280
  558. net.ipv4.icmp_echo_ignore_broadcasts = 1
  559. net.ipv4.conf.all.accept_source_route = 0
  560. net.ipv4.conf.all.accept_redirects = 0
  561. net.ipv4.conf.all.secure_redirects = 0
  562. net.ipv4.conf.all.log_martians = 1
  563. net.ipv4.conf.default.accept_source_route = 0
  564. net.ipv4.conf.default.accept_redirects = 0
  565. net.ipv4.conf.default.secure_redirects = 0
  566. net.ipv4.icmp_echo_ignore_broadcasts = 1
  567. net.ipv4.icmp_ignore_bogus_error_responses = 1
  568. net.ipv4.tcp_syncookies = 1
  569. net.ipv4.conf.all.rp_filter = 1
  570. net.ipv4.conf.default.rp_filter = 1
  571. net.ipv4.tcp_mtu_probing = 1
  572. mode: '000600'
  573. owner: root
  574. group: root
  575. commands:
  576. 00-sed-instance-specific-settings:
  577. command: >-
  578. ipaddr=$(curl 169.254.169.254/latest/meta-data/local-ipv4) &&
  579. sed -i -e "s/{PRIVATE_IP}/${ipaddr}/" /etc/quagga/zebra.conf &&
  580. sed -i -e "s/{PRIVATE_IP}/${ipaddr}/" /etc/quagga/bgpd.conf &&
  581. hostname=$(curl 169.254.169.254/latest/meta-data/local-hostname) &&
  582. sed -i -e "s/{HOSTNAME}/${hostname}/" /etc/quagga/zebra.conf
  583. 01-load-sysctl-changes:
  584. command: sysctl -p /etc/sysctl.conf
  585. 02-enable-ip-forwarding:
  586. command: >-
  587. sysctl -w net.ipv4.ip_forward=1 &&
  588. sysctl -w net.ipv4.conf.eth0.disable_xfrm=1 &&
  589. sysctl -w net.ipv4.conf.eth0.disable_policy=1
  590. 03-enable-start-ntpd:
  591. command: >-
  592. systemctl enable ntpd &&
  593. systemctl start ntpd
  594. 04-enable-start-strongswan:
  595. command: >-
  596. systemctl enable strongswan &&
  597. systemctl start strongswan
  598. 05-enable-start-zebra:
  599. command: >-
  600. systemctl enable zebra &&
  601. systemctl start zebra
  602. 06-enable-start-bgpd:
  603. command: >-
  604. systemctl enable bgpd &&
  605. systemctl start bgpd
  606. rVpnGatewayEipAssociation:
  607. Type: AWS::EC2::EIPAssociation
  608. Condition: cUseElasticIp
  609. Properties:
  610. AllocationId: !Ref pEipAllocationId
  611. InstanceId: !Ref rVpnGateway
  612. rVpnGateway:
  613. Type: AWS::EC2::Instance
  614. Properties:
  615. LaunchTemplate:
  616. LaunchTemplateId:
  617. Ref: rLaunchTemplate
  618. Version:
  619. Fn::GetAtt:
  620. [ rLaunchTemplate, LatestVersionNumber ]
  621. NetworkInterfaces:
  622. - DeviceIndex: '0'
  623. SubnetId: !Ref pSubnetId
  624. SourceDestCheck: false
  625. Tags:
  626. - Key: Name
  627. Value: !Sub '${pSystem}-${pApp}-${pEnvPurpose}'
  628. rVpnGatewayWaitHandle:
  629. Type: AWS::CloudFormation::WaitConditionHandle
  630. rVpnGatewayWaitCondition1:
  631. Type: AWS::CloudFormation::WaitCondition
  632. DependsOn: rVpnGateway
  633. Properties:
  634. Handle:
  635. Ref: rVpnGatewayWaitHandle
  636. Timeout: '300'
  637. Count: 1
  638. rRole:
  639. Type: AWS::IAM::Role
  640. Properties:
  641. RoleName: !Sub '${pOrg}-${pSystem}-${pApp}-${pEnvPurpose}-svc-cloud-watch-ssm'
  642. Path: !Sub '/${pOrg}/${pSystem}/${pApp}/'
  643. AssumeRolePolicyDocument:
  644. Version: 2012-10-17
  645. Statement:
  646. -
  647. Effect: Allow
  648. Principal:
  649. Service: ec2.amazonaws.com
  650. Action: sts:AssumeRole
  651. ManagedPolicyArns:
  652. - arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
  653. - arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy
  654. rInstanceProfile:
  655. Type: AWS::IAM::InstanceProfile
  656. Properties:
  657. InstanceProfileName: !Sub '${pSystem}-${pApp}-${pEnvPurpose}'
  658. Path: !Sub '/${pOrg}/${pSystem}/${pApp}/'
  659. Roles:
  660. - !Ref rRole
  661. rCloudWatchLogsAgentGroup:
  662. Type: AWS::Logs::LogGroup
  663. Properties:
  664. LogGroupName: !Sub '/${pSystem}/${pApp}/ec2/${pEnvPurpose}'
  665. RetentionInDays: 1