Closed
Description
Hi,
I am setting up an OpenSearch / ElasticSeach cluster as an addon. It works but I had a couple questions:
- There is a part of the code that expose port 443, I'm not 100% sure what the code does to be honest, so im just checking it isn't doing something silly and insecure. I'm hoping it is just exposing port 443 for the ElasticSearch domain I am creating in the Cloudformation.
- I had to hardcode my private subnet.
HARDCODED-SUBNET-ID
in the below code. Would you have a suggestion to not have to hardcode this?
SubnetIds:
- 'HARDCODED-SUBNET-ID'
I also tried doing this, but ran into an error in Cloudformation saying "Invalid request provided: You must specify exactly one subnet."
SubnetIds: !Split [ ',', { 'Fn::ImportValue': !Sub '${App}-${Env}-PrivateSubnets' } ]
Parameters:
App:
Type: String
Description: Your application's name.
Env:
Type: String
Description: The environment name your service, job, or workflow is being deployed to.
Name:
Type: String
Description: The name of the service, job, or workflow being deployed.
Resources:
# Security group to add the DB to the VPC,
# and to allow the Fargate containers to talk to DB
ElasticSecurityGroup:
Metadata:
'aws:copilot:description': 'A security group to access the DB cluster'
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: "DB Security Group"
VpcId: { 'Fn::ImportValue': !Sub '${App}-${Env}-VpcId' }
# Enable ingress from other ECS services created within the environment.
ElasticSearchIngress:
Metadata:
'aws:copilot:description': 'Allow ingress from containers in my application to the DB cluster'
Type: AWS::EC2::SecurityGroupIngress
Properties:
Description: Ingress from Fargate containers
GroupId: !Ref 'ElasticSecurityGroup'
IpProtocol: tcp
FromPort: 443
ToPort: 443
SourceSecurityGroupId: { 'Fn::ImportValue': !Sub '${App}-${Env}-EnvironmentSecurityGroup' }
# The cluster itself.
ElasticsearchDomain:
Type: 'AWS::OpenSearchService::Domain'
Properties:
AccessPolicies:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
AWS: '*'
Action:
- 'es:ESHttp*'
Resource: !Sub 'arn:aws:es:${AWS::Region}:${AWS::AccountId}:domain/opensearch-db/*'
DomainName: opensearch-db
EngineVersion: 'OpenSearch_1.0'
EBSOptions:
EBSEnabled: true
VolumeSize: 10
VolumeType: gp2
ClusterConfig:
DedicatedMasterEnabled: false
InstanceCount: 1
InstanceType: t3.medium.search
VPCOptions:
SecurityGroupIds:
- !Ref ElasticSecurityGroup
SubnetIds:
- 'HARDCODED-SUBNET-ID'
ElasticEndpointAddressParam:
Type: AWS::SSM::Parameter
Properties:
Name: !Sub "/copilot/${App}/${Env}/secrets/OPENSEARCH_ENDPOINT"
Type: String
Value: !GetAtt ElasticsearchDomain.DomainEndpoint
Outputs:
ElasticSecurityGroup:
Description: Security group for DB
Value: !Ref ElasticSecurityGroup