├── LICENSE ├── README.md ├── diagrams ├── aws-1-instance.png └── aws-3-instance-cluster.png ├── iam.tf ├── instances.tf ├── keys.tf ├── loadbalancer.tf ├── neo4j.tftpl ├── network.tf ├── outputs.tf ├── security.tf └── variables.tf /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2022-2023 Neo4j 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # neo4j-aws-terraform 2 | 3 | This repository hosts a terraform module for the installation of an environment in AWS EC2, running neo4j. 4 | 5 | ## Usage 6 | The terraform code hosted in this repository can be easily used by creating a parent module on your local machine, in a main.tf file as shown below. 7 | (More information about terraform modules can be found on [this](https://developer.hashicorp.com/terraform/language/modules) page) 8 | 9 | Note the `source` parameter can be used to *either* point directly to this repository or a local copy of the terraform module. 10 | 11 | The command [`ssh-keygen`](https://linux.die.net/man/1/ssh-keygen) can be used to generate a keypair. The private key should not be shared, and its file location should be the value for `private_key_path`. The contents of the public key should be given as the value for `public_key_path` 12 | 13 | ~~~ 14 | #main.tf file for deploying neo4j-terraform 15 | module "neo4j-environment" { 16 | source = "github.com/neo4j/neo4j-aws-terraform" 17 | //source = "../neo4j-terraform" 18 | 19 | //Required values (no defaults are provided) 20 | neo4j_password = "pw_for_neo4j_user" 21 | public_key_value = "ssh-rsa AAAAB3NzaC1A.....b+oTz7tb0WF2aiOPp0=" 22 | private_key_path = "~/.ssh/my-ssh-key" 23 | 24 | //The following Optional values can be omitted if the defaults are satisfactory. 25 | 26 | //Default is "t3.medium" 27 | instance_type = "t3.medium" 28 | 29 | //Default is 3. Valid values are 1, or 3 -> 10 (inclusive) 30 | node_count = 3 31 | 32 | //Default is "10.0.0.0/16" 33 | vpc_base_cidr = "10.0.0.0/16" 34 | 35 | //Default is "0.0.0.0/0" 36 | ssh_source_cidr = "0.0.0.0/0" 37 | neo4j_source_cidr = "0.0.0.0/0" 38 | 39 | //Default is false 40 | install_gds = "false" 41 | 42 | //Default is false 43 | install_bloom = "false" 44 | 45 | //Default is true 46 | install_apoc = "true" 47 | 48 | //Default is "neo4j-tf-cloud" 49 | env_prefix = "my-neo4j-env" 50 | 51 | //Default is "us-east-1" 52 | target_region = "us-east-1" 53 | 54 | //Default is "None" 55 | gds_key = "None" 56 | 57 | //Default is "None" 58 | bloom_key = "None" 59 | } 60 | 61 | output "ssh_commands" { 62 | value = module.neo4j-environment.ssh_commands 63 | } 64 | 65 | output "neo4j_browser_url" { 66 | value = module.neo4j-environment.neo4j_browser_url 67 | } 68 | ~~~ 69 | 70 | ## Cloud Topology 71 | Assuming that defaults were used, the following resources are created by the terraform module: 72 | 73 | Users are reminded that the deployment of cloud resources will incur costs. 74 | 75 | - 1 VPC, with a CIDR Range of 10.0.0.0/16 76 | - 3 Subnets, distributed evenly across 3 Availability zones, with the following CIDR Ranges: 77 | - 10.0.1.0/24 78 | - 10.0.2.0/24 79 | - 10.0.3.0/24 80 | - 1, or between 3 and 10 EC2 instances (Depending on whether a single instance, or an autonomous cluster is selected) 81 | - 1 Network (Layer 4) Load Balancer 82 | 83 | ### Diagram: Single Neo4j Instance on AWS 84 | ![image](diagrams/aws-1-instance.png) 85 | 86 | ### Diagram: Three Node Neo4j Cluster on AWS 87 | ![image](diagrams/aws-3-instance-cluster.png) 88 | 89 | ## Prerequisites 90 | 91 | ### Terraform and AWS CLI configuration 92 | In order to use this module, terraform needs to be properly installed and configured. Whilst this is out of the scope of this README file, an example `provider.tf` file is shown below. The [official terraform documentation](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli) explains how to get terraform up and running on a local machine. Alternatively, [Terraform Cloud](https://developer.hashicorp.com/terraform/tutorials/cloud-get-started) is another option. 93 | 94 | ~~~ 95 | //Configure the terraform backend (S3) and aws provider 96 | terraform { 97 | backend "s3" { 98 | bucket = "" 99 | key = "terraform.tfstate" 100 | region = "us-east-1" 101 | } 102 | 103 | required_providers { 104 | aws = { 105 | source = "hashicorp/aws" 106 | } 107 | } 108 | } 109 | 110 | //Specify which AWS region and profile should be used by default 111 | provider "aws" { 112 | region = "us-east-1" 113 | } 114 | ~~~ 115 | 116 | ### AWS Permisions 117 | 118 | As part of this implementation, an IAM role is created for the EC2 instances which allows them to 'tag' the Network Load Balancer with the current version of neo4j. This functionality has been put in place as part of future development plans to ensure that any new nodes which are added to the cluster install the same version of Neo4j as the existing instances. Users will therefore need to ensure that their AWS account has sufficient permissions to be able to perform the following. 119 | - Create an IAM role 120 | - Attach the IAM role to EC2 instances 121 | - Create a policy for the role which includes the following permissions: 122 | elasticloadbalancing:AddTags 123 | elasticloadbalancing:DescribeTags 124 | elasticloadbalancingv2:AddTags 125 | elasticloadbalancingv2:DescribeTags 126 | 127 | 128 | The IAM role, policy and permissions are all created within the [iam.tf](iam.tf) file. 129 | 130 | The role is attached to the instance by the following entry within the [instances.tf](instances.tf) file: 131 | ``` iam_instance_profile = aws_iam_instance_profile.neo4j_instance_profile.name ``` 132 | 133 | Here is an example line from [neo4j.tftpl](neo4j.tftpl) showing the EC2 instance tagging the load balancer with the current version of neo4j: 134 | ``` aws elbv2 add-tags --resource-arns $LB_ARN --tags Key=Neo4j_Built_Version,Value=$(/usr/share/neo4j/bin/neo4j --version) --region=$TARGET_REGION ``` 135 | 136 | 137 | ## Limitations 138 | 139 | Currently, the following limitations apply: 140 | - Only Neo4j v5 is supported. Support for 4.4 will follow. 141 | - The addition of nodes to a neo4j cluster is currently not supported. The desired number of nodes in a cluster must be selected prior to deployment, using the node_count variable 142 | - SSL (https) has not been included in this initial release 143 | - The environment uses a network load balancer in AWS. This means the EC2 instances will also be directly accessible from whichever address ranges are configure in var.ssh_source_cidr 144 | -------------------------------------------------------------------------------- /diagrams/aws-1-instance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/neo4j-partners/neo4j-aws-terraform/ba1a02663927ac05ac7ce060d949b8bd5df8091f/diagrams/aws-1-instance.png -------------------------------------------------------------------------------- /diagrams/aws-3-instance-cluster.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/neo4j-partners/neo4j-aws-terraform/ba1a02663927ac05ac7ce060d949b8bd5df8091f/diagrams/aws-3-instance-cluster.png -------------------------------------------------------------------------------- /iam.tf: -------------------------------------------------------------------------------- 1 | resource "aws_iam_role" "neo4j_ec2_role" { 2 | name = "${var.env_prefix}-role" 3 | assume_role_policy = local.assume_role_policy 4 | } 5 | 6 | resource "aws_iam_instance_profile" "neo4j_instance_profile" { 7 | name = "${var.env_prefix}-profile" 8 | role = aws_iam_role.neo4j_ec2_role.name 9 | } 10 | 11 | resource "aws_iam_role_policy_attachment" "ec2_ro_policy_attachment" { 12 | role = aws_iam_role.neo4j_ec2_role.name 13 | policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess" 14 | } 15 | 16 | resource "aws_iam_role_policy_attachment" "neo4j_nlb_tagging_policy" { 17 | role = aws_iam_role.neo4j_ec2_role.name 18 | policy_arn = aws_iam_policy.neo4j_nlb_tagging_policy.arn 19 | } 20 | 21 | resource "aws_iam_policy" "neo4j_nlb_tagging_policy" { 22 | name = "neo4j_nlb_tagging_policy" 23 | description = "Policy for tagging Network Load Balancer" 24 | policy = data.aws_iam_policy_document.neo4j_nlb_tagging_policy_document.json 25 | } 26 | 27 | data "aws_iam_policy_document" "neo4j_nlb_tagging_policy_document" { 28 | depends_on = [ 29 | aws_lb.neo4j_lb 30 | ] 31 | 32 | statement { 33 | effect = "Allow" 34 | actions = [ 35 | "elasticloadbalancing:AddTags", 36 | "elasticloadbalancing:DescribeTags", 37 | "elasticloadbalancingv2:AddTags", 38 | "elasticloadbalancingv2:DescribeTags", 39 | ] 40 | 41 | resources = [ 42 | aws_lb.neo4j_lb.arn 43 | ] 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /instances.tf: -------------------------------------------------------------------------------- 1 | resource "aws_instance" "neo4j_instance" { 2 | count = var.node_count 3 | ami = var.neo4j-ami-list["${var.neo4j_version}"]["${var.target_region}"] 4 | 5 | instance_type = var.instance_type 6 | key_name = aws_key_pair.neo4j_ec2_key.id 7 | 8 | subnet_id = element(aws_subnet.neo4j_public_subnet.*.id, count.index % 3) 9 | vpc_security_group_ids = ["${aws_security_group.neo4j_sg.id}"] 10 | iam_instance_profile = aws_iam_instance_profile.neo4j_instance_profile.name 11 | depends_on = [aws_lb.neo4j_lb] 12 | 13 | user_data = templatefile( 14 | "${path.module}/neo4j.tftpl", 15 | { 16 | install_gds = var.install_gds 17 | install_bloom = var.install_bloom 18 | gds_key = var.gds_key 19 | bloom_key = var.bloom_key 20 | neo4j_password = var.neo4j_password 21 | install_apoc = var.install_apoc 22 | node_count = var.node_count 23 | lb_fqdn = aws_lb.neo4j_lb.dns_name 24 | lb_arn = aws_lb.neo4j_lb.arn 25 | neo4j_version = var.neo4j_version 26 | target_region = var.target_region 27 | env_prefix = var.env_prefix 28 | } 29 | ) 30 | 31 | tags = { 32 | "Name" = "${var.env_prefix}-instance" 33 | "Terraform" = true 34 | } 35 | 36 | // only set to true when developing/debugging. tf default = false 37 | user_data_replace_on_change = false 38 | 39 | // don't force-recreate instance if only user data changes 40 | lifecycle { 41 | ignore_changes = [user_data] 42 | } 43 | } -------------------------------------------------------------------------------- /keys.tf: -------------------------------------------------------------------------------- 1 | resource "random_id" "key_pair_name" { 2 | byte_length = 4 3 | prefix = "${var.env_prefix}-ssh-key" 4 | } 5 | 6 | resource "aws_key_pair" "neo4j_ec2_key" { 7 | key_name = random_id.key_pair_name.hex 8 | public_key = var.public_key_value 9 | 10 | tags = { 11 | "Name" = "${var.env_prefix}-neo4j-ec2-key" 12 | "Terraform" = true 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /loadbalancer.tf: -------------------------------------------------------------------------------- 1 | resource "aws_lb" "neo4j_lb" { 2 | name = "${var.env_prefix}-nlb" 3 | internal = false 4 | load_balancer_type = "network" 5 | 6 | subnets = [for subnet in aws_subnet.neo4j_public_subnet : subnet.id] 7 | 8 | tags = { 9 | "Name" = "${var.env_prefix}-nlb" 10 | "Terraform" = true 11 | } 12 | 13 | lifecycle { 14 | ignore_changes = [ 15 | ] 16 | } 17 | } 18 | 19 | resource "aws_lb_listener" "neo4j_listener_http" { 20 | load_balancer_arn = aws_lb.neo4j_lb.arn 21 | port = "7474" 22 | protocol = "TCP" 23 | 24 | default_action { 25 | target_group_arn = aws_lb_target_group.neo4j_http_lb_tg.arn 26 | type = "forward" 27 | } 28 | } 29 | 30 | resource "aws_lb_listener" "neo4j_listener_bolt" { 31 | load_balancer_arn = aws_lb.neo4j_lb.arn 32 | port = "7687" 33 | protocol = "TCP" 34 | 35 | default_action { 36 | target_group_arn = aws_lb_target_group.neo4j_bolt_lb_tg.arn 37 | type = "forward" 38 | } 39 | } 40 | 41 | resource "aws_lb_target_group" "neo4j_http_lb_tg" { 42 | name = "${var.env_prefix}-http-tg" 43 | port = 7474 44 | protocol = "TCP" 45 | vpc_id = aws_vpc.neo4j_vpc.id 46 | } 47 | 48 | resource "aws_lb_target_group" "neo4j_bolt_lb_tg" { 49 | name = "${var.env_prefix}-bolt-tg" 50 | port = 7687 51 | protocol = "TCP" 52 | vpc_id = aws_vpc.neo4j_vpc.id 53 | } 54 | 55 | resource "aws_lb_target_group_attachment" "neo4j_http_lb_tg_attachment" { 56 | count = var.node_count 57 | target_group_arn = aws_lb_target_group.neo4j_http_lb_tg.arn 58 | target_id = aws_instance.neo4j_instance[count.index].id 59 | port = 7474 60 | } 61 | 62 | resource "aws_lb_target_group_attachment" "neo4j_bolt_lb_tg_attachment" { 63 | count = var.node_count 64 | target_group_arn = aws_lb_target_group.neo4j_bolt_lb_tg.arn 65 | target_id = aws_instance.neo4j_instance[count.index].id 66 | port = 7687 67 | } -------------------------------------------------------------------------------- /neo4j.tftpl: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | ## 0 - Wait 30 seconds before starting script execution 4 | sleep 30 5 | 6 | #ONLY USED FOR TESTING 7 | #OVERRIDE_VERSION=5.2.0 8 | 9 | ## 1 - Variable Setting and Test 10 | INSTALL_GDS=${install_gds} 11 | INSTALL_BLOOM=${install_bloom} 12 | GDS_KEY=${gds_key} 13 | BLOOM_KEY=${bloom_key} 14 | NEO4J_PASSWORD=${neo4j_password} 15 | NODE_COUNT=${node_count} 16 | LB_FQDN=${lb_fqdn} 17 | NEO4J_VERSION=${neo4j_version} 18 | TARGET_REGION=${target_region} 19 | LB_ARN=${lb_arn} 20 | THIS_INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) 21 | ENV_PREFIX=${env_prefix} 22 | 23 | function aws_get_private_ips { 24 | aws ec2 describe-instances\ 25 | --output=text\ 26 | --region=$TARGET_REGION\ 27 | --filters Name=tag:Terraform,Values=true\ 28 | --filters Name=tag:Name,Values=$ENV_PREFIX-instance\ 29 | --query "Reservations[].Instances[].PrivateIpAddress" 30 | } 31 | 32 | PRIVATE_IPS=$(aws_get_private_ips) 33 | 34 | for ip in $PRIVATE_IPS ; do 35 | private_ip_array+=("$ip:5000,") 36 | done 37 | 38 | CORE_MEMBERS=$(echo $${private_ip_array[@]} | sed 's/,$//' | sed 's/ //g') 39 | 40 | function existing_version_tag { 41 | aws elbv2 describe-tags\ 42 | --resource-arns $LB_ARN\ 43 | --region=$TARGET_REGION\ 44 | --query 'TagDescriptions[*].Tags[?Key==`Neo4j_Built_Version`].Value'\ 45 | --output=text 46 | } 47 | 48 | EXISTING_VERSION_TAG="$(existing_version_tag)" 49 | 50 | ## 2 - Install Neo4j using yum 51 | echo " - [ Installing Graph Database ] - " 52 | export NEO4J_ACCEPT_LICENSE_AGREEMENT=yes 53 | 54 | if [[ ! -z $OVERRIDE_VERSION && $OVERRIDE_VERSION =~ ^[0-9]\.[0-9]\.[0-9]$ ]]; then 55 | echo " - [ Installing OVERRIDE_VERSION: $OVERRIDE_VERSION ] - " 56 | yum install -y neo4j-enterprise-$OVERRIDE_VERSION 57 | sleep 1 58 | aws elbv2 add-tags --resource-arns $LB_ARN --tags Key=Neo4j_Built_Version,Value=$(/usr/share/neo4j/bin/neo4j --version) --region=$TARGET_REGION 59 | elif [[ $EXISTING_VERSION_TAG =~ ^[0-9]\.[0-9]\.[0-9]$ ]]; then 60 | echo " - [ Existing version tag already exists, building with version $EXISTING_VERSION_TAG to preserve cluster version ] - " 61 | yum install -y neo4j-enterprise-$EXISTING_VERSION_TAG 62 | 63 | else 64 | echo "No valid existing version set - assuming initial deployment" 65 | PACKAGE_VERSION=$(curl --fail http://versions.neo4j-templates.com/target.json | jq -r ".aws[\"$NEO4J_VERSION\"]" || echo "") 66 | if [[ ! -z $PACKAGE_VERSION && $PACKAGE_VERSION != "null" ]]; then 67 | echo " - [ Found PACKAGE_VERSION from http://versions.neo4j-templates.com : PACKAGE_VERSION=$PACKAGE_VERSION ] - " 68 | yum install -y neo4j-enterprise-$PACKAGE_VERSION 69 | sleep 1 70 | aws elbv2 add-tags --resource-arns $LB_ARN --tags Key=Neo4j_Built_Version,Value=$(/usr/share/neo4j/bin/neo4j --version) --region=$TARGET_REGION 71 | else 72 | echo '- [ Failed to resolve Neo4j version from http://versions.neo4j-templates.com, using PACKAGE_VERSION=latest ] - ' 73 | yum install -y "neo4j-enterprise" 74 | aws elbv2 add-tags --resource-arns $LB_ARN --tags Key=Neo4j_Built_Version,Value=$(/usr/share/neo4j/bin/neo4j --version) --region=$TARGET_REGION 75 | fi 76 | fi 77 | 78 | systemctl enable neo4j 79 | if [[ "$PACKAGE_VERSION" == "latest" ]]; then 80 | PACKAGE_VERSION=$(/usr/share/neo4j/bin/neo4j --version) 81 | fi 82 | 83 | ## 3 - Extension Config 84 | echo " - [ Configuring extensions and security in neo4j.conf ] - " 85 | sed -i s~#server.unmanaged_extension_classes=org.neo4j.examples.server.unmanaged=/examples/unmanaged~server.unmanaged_extension_classes=com.neo4j.bloom.server=/bloom,semantics.extension=/rdf~g /etc/neo4j/neo4j.conf 86 | sed -i s/#dbms.security.procedures.unrestricted=my.extensions.example,my.procedures.*/dbms.security.procedures.unrestricted=gds.*,apoc.*,bloom.*/g /etc/neo4j/neo4j.conf 87 | echo "dbms.security.http_auth_allowlist=/,/browser.*,/bloom.*" >> /etc/neo4j/neo4j.conf 88 | echo "dbms.security.procedures.allowlist=apoc.*,gds.*,bloom.*" >> /etc/neo4j/neo4j.conf 89 | 90 | ## 4 - Neo4j Main Configuration 91 | echo " - [ Neo4j Main (Network & Cluster Configuration ] - " 92 | THIS_PRIVATE_IP="$(hostname -i | awk '{print $NF}')" 93 | sed -i s/#server.default_listen_address=0.0.0.0/server.default_listen_address=0.0.0.0/g /etc/neo4j/neo4j.conf 94 | sed -i s/#server.default_advertised_address=localhost/server.default_advertised_address="$LB_FQDN"/g /etc/neo4j/neo4j.conf 95 | sed -i s/#server.discovery.advertised_address=:5000/server.discovery.advertised_address="$THIS_PRIVATE_IP":5000/g /etc/neo4j/neo4j.conf 96 | sed -i s/#server.cluster.advertised_address=:6000/server.cluster.advertised_address="$THIS_PRIVATE_IP":6000/g /etc/neo4j/neo4j.conf 97 | sed -i s/#server.cluster.raft.advertised_address=:7000/server.cluster.raft.advertised_address="$THIS_PRIVATE_IP":7000/g /etc/neo4j/neo4j.conf 98 | sed -i s/#server.routing.advertised_address=:7688/server.routing.advertised_address="$THIS_PRIVATE_IP":7688/g /etc/neo4j/neo4j.conf 99 | sed -i s/#server.discovery.listen_address=:5000/server.discovery.listen_address="$THIS_PRIVATE_IP":5000/g /etc/neo4j/neo4j.conf 100 | sed -i s/#server.routing.listen_address=0.0.0.0:7688/server.routing.listen_address="$THIS_PRIVATE_IP":7688/g /etc/neo4j/neo4j.conf 101 | sed -i s/#server.cluster.listen_address=:6000/server.cluster.listen_address="$THIS_PRIVATE_IP":6000/g /etc/neo4j/neo4j.conf 102 | sed -i s/#server.cluster.raft.listen_address=:7000/server.cluster.raft.listen_address="$THIS_PRIVATE_IP":7000/g /etc/neo4j/neo4j.conf 103 | sed -i s/#server.bolt.listen_address=:7687/server.bolt.listen_address="$THIS_PRIVATE_IP":7687/g /etc/neo4j/neo4j.conf 104 | sed -i s/#server.bolt.advertised_address=:7687/server.bolt.advertised_address="$THIS_PRIVATE_IP":7687/g /etc/neo4j/neo4j.conf 105 | neo4j-admin server memory-recommendation >> /etc/neo4j/neo4j.conf 106 | echo "server.metrics.enabled=true" >> /etc/neo4j/neo4j.conf 107 | echo "server.metrics.jmx.enabled=true" >> /etc/neo4j/neo4j.conf 108 | echo "server.metrics.prefix=neo4j" >> /etc/neo4j/neo4j.conf 109 | echo "server.metrics.filter=*" >> /etc/neo4j/neo4j.conf 110 | echo "server.metrics.csv.interval=5s" >> /etc/neo4j/neo4j.conf 111 | echo "dbms.routing.default_router=SERVER" >> /etc/neo4j/neo4j.conf 112 | if [[ $NODE_COUNT == 1 ]]; then 113 | echo "- [ Running on a single node ] -" 114 | else 115 | echo " - [ Running on multiple nodes. Configuring membership in neo4j.conf ] - " 116 | sed -i s/#initial.dbms.default_primaries_count=1/initial.dbms.default_primaries_count=3/g /etc/neo4j/neo4j.conf 117 | sed -i s/#initial.dbms.default_secondaries_count=0/initial.dbms.default_secondaries_count=$(expr $NODE_COUNT - 3)/g /etc/neo4j/neo4j.conf 118 | sed -i s/#server.bolt.listen_address=:7687/server.bolt.listen_address="$THIS_PRIVATE_IP":7687/g /etc/neo4j/neo4j.conf 119 | echo "dbms.cluster.minimum_initial_system_primaries_count=$NODE_COUNT" >> /etc/neo4j/neo4j.conf 120 | sed -i s/#dbms.cluster.discovery.endpoints=localhost:5000,localhost:5001,localhost:5002/dbms.cluster.discovery.endpoints=$CORE_MEMBERS/g /etc/neo4j/neo4j.conf 121 | fi 122 | 123 | ## 5 - Install Graph Data Science 124 | if [[ "$INSTALL_GDS" == true && $NODE_COUNT == 1 ]]; then 125 | echo " - [ Installing Graph Data Science] - " 126 | cp /var/lib/neo4j/products/neo4j-graph-data-science-*.jar /var/lib/neo4j/plugins 127 | fi 128 | if [[ $GDS_KEY != None ]]; then 129 | echo " - [ Writing GDS license key ] - " 130 | mkdir -p /etc/neo4j/licenses 131 | echo "$GDS_KEY" > /etc/neo4j/licenses/neo4j-gds.license 132 | sed -i '$a gds.enterprise.license_file=/etc/neo4j/licenses/neo4j-gds.license' /etc/neo4j/neo4j.conf 133 | fi 134 | 135 | ## 6 - Install Bloom 136 | if [[ $INSTALL_BLOOM == true ]]; then 137 | echo " - [ Installing Bloom ] - " 138 | cp /var/lib/neo4j/products/bloom-plugin-*.jar /var/lib/neo4j/plugins 139 | fi 140 | if [[ $BLOOM_KEY != None ]]; then 141 | echo " - [ Writing Bloom license key ] - " 142 | mkdir -p /etc/neo4j/licenses 143 | echo "$BLOOM_KEY" > /etc/neo4j/licenses/neo4j-bloom.license 144 | sed -i '$a dbms.bloom.license_file=/etc/neo4j/licenses/neo4j-bloom.license' /etc/neo4j/neo4j.conf 145 | fi 146 | 147 | ## 7 - Start Neo4j 148 | echo " - [ Starting Neo4j ] - " 149 | service neo4j start 150 | neo4j-admin dbms set-initial-password "$NEO4J_PASSWORD" -------------------------------------------------------------------------------- /network.tf: -------------------------------------------------------------------------------- 1 | resource "aws_vpc" "neo4j_vpc" { 2 | cidr_block = var.vpc_base_cidr 3 | enable_dns_support = true 4 | enable_dns_hostnames = true 5 | 6 | tags = { 7 | "Name" = "${var.env_prefix}-vpc" 8 | "Terraform" = true 9 | } 10 | } 11 | 12 | resource "aws_subnet" "neo4j_public_subnet" { 13 | count = var.subnet_qty 14 | vpc_id = aws_vpc.neo4j_vpc.id 15 | cidr_block = cidrsubnet(var.vpc_base_cidr, 8, count.index + 1) 16 | availability_zone = join("", ["${var.target_region}", "${var.availability_zones[count.index]}"]) 17 | map_public_ip_on_launch = true 18 | 19 | tags = { 20 | "Name" = "${var.env_prefix}-public-subnet-${var.availability_zones[count.index]}" 21 | "Terraform" = true 22 | } 23 | } 24 | 25 | resource "aws_internet_gateway" "neo4j_igw" { 26 | vpc_id = aws_vpc.neo4j_vpc.id 27 | 28 | tags = { 29 | "Name" = "${var.env_prefix}-vpc-igw" 30 | "Terraform" = true 31 | } 32 | } 33 | 34 | resource "aws_route_table" "neo4j_public_subnet_rt" { 35 | vpc_id = aws_vpc.neo4j_vpc.id 36 | tags = { 37 | "Name" = "${var.env_prefix}-public-subnet-rt" 38 | "Terraform" = true 39 | } 40 | } 41 | 42 | resource "aws_route" "neo4j_public_subnet_route" { 43 | route_table_id = aws_route_table.neo4j_public_subnet_rt.id 44 | destination_cidr_block = "0.0.0.0/0" 45 | gateway_id = aws_internet_gateway.neo4j_igw.id 46 | } 47 | 48 | resource "aws_route_table_association" "neo4j_public_route_assoc" { 49 | count = var.subnet_qty 50 | subnet_id = aws_subnet.neo4j_public_subnet[count.index].id 51 | route_table_id = aws_route_table.neo4j_public_subnet_rt.id 52 | } 53 | -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | output "ssh_commands" { 2 | value = [ 3 | for key, value in aws_instance.neo4j_instance[*].public_ip : "ssh -o StrictHostKeyChecking=no -i ${var.private_key_path} ec2-user@${value}" 4 | ] 5 | } 6 | 7 | output "neo4j_browser_url" { 8 | value = "http://${aws_lb.neo4j_lb.dns_name}:7474" 9 | } 10 | 11 | output "neo4j_password" { 12 | value = var.neo4j_password 13 | } 14 | 15 | output "instance_type" { 16 | value = var.instance_type 17 | } 18 | 19 | output "public_key_value" { 20 | value = var.public_key_value 21 | } 22 | 23 | output "private_key_path" { 24 | value = var.private_key_path 25 | } 26 | 27 | output "node_count" { 28 | value = var.node_count 29 | } 30 | 31 | output "vpc_base_cidr" { 32 | value = var.vpc_base_cidr 33 | } 34 | 35 | output "install_gds" { 36 | value = var.install_gds 37 | } 38 | 39 | output "install_bloom" { 40 | value = var.install_bloom 41 | } 42 | 43 | output "install_apoc" { 44 | value = var.install_apoc 45 | } 46 | 47 | output "env_prefix" { 48 | value = var.env_prefix 49 | } 50 | 51 | output "target_region" { 52 | value = var.target_region 53 | } 54 | 55 | output "gds_key" { 56 | value = var.gds_key 57 | } 58 | 59 | output "bloom_key" { 60 | value = var.bloom_key 61 | } -------------------------------------------------------------------------------- /security.tf: -------------------------------------------------------------------------------- 1 | resource "aws_security_group" "neo4j_sg" { 2 | name = "${var.env_prefix}_sg" 3 | vpc_id = aws_vpc.neo4j_vpc.id 4 | 5 | // no restrictions on traffic originating from inside the VPC 6 | ingress { 7 | from_port = 0 8 | to_port = 0 9 | protocol = "-1" 10 | cidr_blocks = ["${var.vpc_base_cidr}"] 11 | } 12 | 13 | // no restrictions on ssh traffic from var.ssh_source_cidr 14 | ingress { 15 | from_port = 22 16 | to_port = 22 17 | protocol = "TCP" 18 | cidr_blocks = ["${var.ssh_source_cidr}"] 19 | } 20 | 21 | // no restrictions on neo4j browser traffic from var.neo4j_source_cide 22 | ingress { 23 | from_port = 7474 24 | to_port = 7474 25 | protocol = "TCP" 26 | cidr_blocks = ["${var.neo4j_source_cidr}"] 27 | } 28 | 29 | // no restrictions on neo4j bolt traffic from var.neo4j_source_cide 30 | ingress { 31 | from_port = 7687 32 | to_port = 7687 33 | protocol = "TCP" 34 | cidr_blocks = ["${var.neo4j_source_cidr}"] 35 | } 36 | 37 | // outbound internet access 38 | egress { 39 | from_port = 0 40 | to_port = 0 41 | protocol = "-1" 42 | cidr_blocks = ["0.0.0.0/0"] 43 | } 44 | 45 | tags = { 46 | "Name" = "${var.env_prefix}-sg" 47 | "Terraform" = true 48 | } 49 | } -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | locals { 2 | assume_role_policy = jsonencode({ 3 | Version = "2012-10-17" 4 | Statement = [ 5 | { 6 | Action = "sts:AssumeRole" 7 | Effect = "Allow" 8 | Sid = "" 9 | Principal = { 10 | Service = "ec2.amazonaws.com" 11 | } 12 | } 13 | ] 14 | }) 15 | } 16 | 17 | variable "vpc_base_cidr" { 18 | description = "The base of the address range to be used by the VPC and corresponding Subnets" 19 | type = string 20 | default = "10.10.0.0/16" 21 | validation { 22 | condition = can(regex("^(([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])(\\/([0-9]|[1-2][0-9]|3[0-2]))$", var.vpc_base_cidr)) 23 | error_message = "Invalid IP address provided for the vpc_base_cidr block variable. A valid example would be 10.10.0.0/16" 24 | } 25 | } 26 | 27 | variable "ssh_source_cidr" { 28 | description = "The cidr range which is allowed to connect to the EC2 instances via SSH. Default will be fully open: 0.0.0.0/0" 29 | type = string 30 | default = "0.0.0.0/0" 31 | validation { 32 | condition = can(regex("^(([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])(\\/([0-9]|[1-2][0-9]|3[0-2]))$", var.ssh_source_cidr)) 33 | error_message = "Invalid IP address provided for the ssh_source_cidr variable. A valid example would be 0.0.0.0/16" 34 | } 35 | } 36 | 37 | variable "neo4j_source_cidr" { 38 | description = "The cidr range which is allowed to connect to the neo4j environment. Default will be fully open: 0.0.0.0/0" 39 | type = string 40 | default = "0.0.0.0/0" 41 | validation { 42 | condition = can(regex("^(([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])(\\/([0-9]|[1-2][0-9]|3[0-2]))$", var.neo4j_source_cidr)) 43 | error_message = "Invalid IP address provided for the vpc_neo4j_source_cidr block variable. A valid example would be 0.0.0.0/16" 44 | } 45 | } 46 | 47 | variable "env_prefix" { 48 | description = "A prefix which is useful for tagging and naming" 49 | type = string 50 | } 51 | 52 | variable "neo4j_version" { 53 | description = "The version of neo4j to be installed" 54 | type = string 55 | 56 | validation { 57 | condition = contains(["5"], var.neo4j_version) 58 | error_message = "The only currently supported value is 5 (for Neo4j version 5). Development is ongoing for Neo4j v4.4 (LTS)" 59 | } 60 | 61 | default = 5 62 | } 63 | 64 | variable "target_region" { 65 | description = "The region in which the environment will be deployed" 66 | type = string 67 | default = "us-east-1" 68 | } 69 | 70 | variable "availability_zones" { 71 | description = "A list containing 3 AZs" 72 | type = list(string) 73 | default = ["a", "b", "c"] 74 | } 75 | 76 | variable "subnet_qty" { 77 | description = "The number of subnets in the environment - should remain at 3" 78 | type = number 79 | default = 3 80 | } 81 | 82 | variable "node_count" { 83 | description = "The number of neo4j instances to be deployed" 84 | default = 3 85 | type = number 86 | 87 | validation { 88 | condition = ( 89 | var.node_count > 0 && 90 | var.node_count != 2 && 91 | var.node_count < 11 92 | ) 93 | error_message = "node_count can be 1 or between 3 and 10" 94 | } 95 | } 96 | 97 | variable "instance_type" { 98 | description = "The type of EC2 instances to be deployed" 99 | type = string 100 | default = "t3.medium" 101 | } 102 | 103 | variable "public_key_value" { 104 | description = "The public SSH key, generated on the the local environment" 105 | } 106 | 107 | variable "private_key_path" { 108 | description = "The location of the private SSH key, generated on the the local environment" 109 | } 110 | 111 | variable "install_gds" { 112 | description = "Determine if GDS is required" 113 | type = bool 114 | default = false 115 | } 116 | 117 | variable "install_bloom" { 118 | description = "Determine if Bloom is required" 119 | type = bool 120 | default = false 121 | } 122 | 123 | variable "install_apoc" { 124 | description = "Determine if the APOC library is required" 125 | type = bool 126 | default = true 127 | } 128 | 129 | variable "gds_key" { 130 | description = "License Key for Graph Data Science" 131 | type = string 132 | default = "None" 133 | } 134 | 135 | variable "bloom_key" { 136 | description = "License Key for Bloom" 137 | type = string 138 | default = "None" 139 | } 140 | 141 | variable "neo4j_password" { 142 | description = "Password for the neo4j user" 143 | } 144 | 145 | variable "neo4j-ami-list" { 146 | description = "A map containing the neo4j AMIs" 147 | //type = map(string) 148 | default = { 149 | "4.4" = { 150 | "us-east-1" = "ami-0ebd717756e832d9f" 151 | }, 152 | 153 | "5" = { 154 | "us-east-1" = "ami-0e400af847eb9a531", 155 | "us-east-2" = "ami-0107e71b1207b90c1", 156 | "us-west-1" = "ami-01a47d8951b35c272", 157 | "us-west-2" = "ami-074caf8265c21fd4d", 158 | "ca-central-1" = "ami-07887e0c7c8cd3151", 159 | "eu-central-1" = "ami-01321a57a6c5786c4", 160 | "eu-west-1" = "ami-0f91decef80d7d93b", 161 | "eu-west-2" = "ami-0a7d3edbd43b7eb80", 162 | "eu-west-3" = "ami-084419b01954a6223", 163 | "eu-north-1" = "ami-05fed050da0ade42e", 164 | "eu-south-1" = "ami-02c3742c42b59e7ef", 165 | "ap-southeast-1" = "ami-09a4e375812f71b6a", 166 | "ap-southeast-2" = "ami-0a95c0a2258c32e85", 167 | "ap-southeast-3" = "ami-09093311aa2cb29a8", 168 | "ap-south-1" = "ami-0fceddde2778cc2e0", 169 | "ap-northeast-1" = "ami-0dbecd5269ffad97f", 170 | "ap-northeast-2" = "ami-0e78f50ef5b1e902b", 171 | "ap-northeast-3" = "ami-0ff14d64aef37f40d", 172 | "ap-east-1" = "ami-05f54d0dc0905041d", 173 | "sa-east-1" = "ami-0a431786eba166f75", 174 | "me-south-1" = "ami-0c77db260e85bb422", 175 | "af-south-1" = "ami-041936db2e9c3abc2", 176 | "us-gov-east-1" = "ami-02ed55fa8cb841061", 177 | "us-gov-west-1" = "ami-05f0b2155478f825" 178 | } 179 | } 180 | } 181 | --------------------------------------------------------------------------------