Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Description: Deploying a Logstash service using the S3 output plugin, on AWS Fargate.
- Parameters:
- EnvironmentName:
- Type: String
- Default: LogstashS3Environment
- Description: The name of the environment to add this service to.
- EnvironmentRegion:
- Type: String
- Default: eu-west-2
- S3BucketName:
- Type: String
- Default: logstash-output-here3
- Description: A name for the bucket to which logstash ships logs. Must be unique, to existing buckets. Lowercase only.
- S3BucketCannedACL:
- Type: String
- Default: private
- Description: A name for the bucket to which logstash ships logs. Must be unique, to existing buckets. Lowercase only.
- LogstashClusterName:
- Type: String
- Default: LogstashCluster
- Description: A name for the Cluster.
- LogstashContainerSecurityGroupDescription:
- Type: String
- Default: SecurityGroup blocking all that are not on port 5044(UDP/Syslog) or 80(HealthChecks).
- LogstashContainerName:
- Type: String
- Default: LogstashContainer
- Description: A name for the container.
- ServiceName:
- Type: String
- Default: LogstashService
- Description: A name for the service.
- ImageUrl:
- Type: String
- Default: kalledania/logstashimage:latest
- Description: The url of our custom Logstash docker image.
- SyslogInputPort:
- Type: Number
- Default: 5044
- Description: What port number the application inside the docker container is binding to.
- S3BucketRegion:
- Type: String
- Default: eu-west-2
- Description: The region of the S3 bucket we want to ship logs to.
- NACLAllowedIPS:
- Type: String
- Default: 0.0.0.0/0
- Description: You should probably change this to the specific IP you expect to recieve logs from.
- ContainerCPU:
- Type: String
- Default: 256
- Description: How much CPU to give the container. 1024 is 1 CPU.
- ContainerMemory:
- Type: Number
- Default: 1024
- Description: How much memory in megabytes to give the container. I recommend not going lower than 1024, as 512 occationally experienced out-of-memory errors on container start-up.
- DesiredCount:
- Type: Number
- Default: 1
- Description: How many copies of the service task to run.
- VPCCIDR:
- Type: String
- Default: 10.0.0.0/16
- Subnet1Range:
- Type: String
- Default: 10.0.0.0/24
- Subnet2Range:
- Type: String
- Default: 10.0.1.0/24
- ContainerAllowInboundIPs:
- Type: String
- Default: 0.0.0.0/0
- Description: You should probably change this to the specific IP you expect to recieve logs from.
- AssignPublicIP:
- Type: String
- Default: ENABLED
- Resources:
- LogstashBucket:
- Type: AWS::S3::Bucket
- Properties:
- BucketName: !Ref S3BucketName
- BucketEncryption:
- ServerSideEncryptionConfiguration:
- -
- ServerSideEncryptionByDefault:
- SSEAlgorithm: AES256
- PublicAccessBlockConfiguration:
- BlockPublicAcls: false #true
- BlockPublicPolicy: false #true
- IgnorePublicAcls: false #true
- RestrictPublicBuckets: false #true
- AccessControl: PublicReadWrite #Private
- # Endpoints provide direct access to a service inside the same AWS account, which means our traffic will never reach the public world wide web. We can even use Endpoints to provide access from a private.
- EndpointS3:
- Type: AWS::EC2::VPCEndpoint
- Properties:
- VpcId: !Ref LogstashVPC
- ServiceName: !Sub com.amazonaws.${AWS::Region}.s3
- RouteTableIds:
- - !Ref LogstashRouteTable
- VpcEndpointType: "Gateway"
- #LogstashBucketPolicyPublicReadFromIP:
- # Type: AWS::S3::BucketPolicy
- # Properties:
- # Bucket: !Ref LogstashBucket
- # PolicyDocument:
- # Statement:
- # -
- # Effect: "Allow"
- #Allow only the referenced Arn to PutObject to this bucket..
- # Action: "s3:PutObject"
- # Resource:
- # Fn::Join:
- # - ""
- # - - "arn:aws:s3:::"
- # - !Ref LogstashBucket
- # - "/*"
- # Principal: '*' #!Ref LogstashRolePutToS3
- # Condition:
- # ArnEquals:
- # aws:SourceArn: !Ref LogstashTaskDef #LogstashRolePutToS3
- LogstashPolicyPutToS3:
- Type: AWS::IAM::ManagedPolicy
- Properties:
- ManagedPolicyName: "LogstashPolicyPutToS3"
- PolicyDocument:
- Version: "2012-10-17"
- Statement:
- -
- Effect: "Allow"
- Action: "s3:PutObject"
- Resource: !GetAtt LogstashBucket.Arn
- LogstashRolePutToS3:
- Type: AWS::IAM::Role
- Properties:
- AssumeRolePolicyDocument:
- Version: "2012-10-17"
- Statement:
- -
- Effect: "Allow"
- Principal:
- Service:
- - "ecs-tasks.amazonaws.com"
- Action: "sts:AssumeRole"
- ManagedPolicyArns:
- - !Ref LogstashPolicyPutToS3
- RoleName: "LogstashRolePutToS3"
- LogstashVPC:
- Type: AWS::EC2::VPC
- Properties:
- CidrBlock: !Ref VPCCIDR
- EnableDnsHostnames: "true"
- EnableDnsSupport: "true"
- InstanceTenancy: "default"
- LogstashSub1:
- Type: AWS::EC2::Subnet
- Properties:
- CidrBlock: !Ref Subnet1Range
- MapPublicIpOnLaunch: "true"
- VpcId: !Ref LogstashVPC
- LogstashSub2:
- Type: AWS::EC2::Subnet
- Properties:
- CidrBlock: !Ref Subnet2Range
- MapPublicIpOnLaunch: "true"
- VpcId: !Ref LogstashVPC
- LogstashInternetGateway:
- Type: AWS::EC2::InternetGateway
- LogstashInternetGatewayAttachAction:
- Type: AWS::EC2::VPCGatewayAttachment
- Properties:
- InternetGatewayId: !Ref LogstashInternetGateway
- VpcId: !Ref LogstashVPC
- LogstashRouteTableAssociation1:
- Type: AWS::EC2::SubnetRouteTableAssociation
- Properties:
- SubnetId: !Ref LogstashSub1
- RouteTableId: !Ref LogstashRouteTable
- LogstashRouteTableAssociation2:
- Type: AWS::EC2::SubnetRouteTableAssociation
- Properties:
- SubnetId: !Ref LogstashSub2
- RouteTableId: !Ref LogstashRouteTable
- LogstashRouteTable:
- Type: AWS::EC2::RouteTable
- Properties:
- VpcId: !Ref LogstashVPC
- LogstashVPCInternetGatewayRoute:
- Type: AWS::EC2::Route
- Properties:
- DestinationCidrBlock: 0.0.0.0/0
- GatewayId: !Ref LogstashInternetGateway
- RouteTableId: !Ref LogstashRouteTable
- LogstashCluster:
- Type: AWS::ECS::Cluster
- Properties:
- ClusterName: !Ref LogstashClusterName
- LogGroup:
- Type: AWS::Logs::LogGroup
- Properties:
- LogGroupName: !Join ['', [/ecs/, !Ref ServiceName, TaskDefinition]]
- PolicyECSExecution:
- Type: AWS::IAM::ManagedPolicy
- Properties:
- ManagedPolicyName: "PolicyECSExecution"
- PolicyDocument:
- Version: "2012-10-17"
- Statement:
- -
- Effect: "Allow"
- Action:
- - ecr:GetAuthorizationToken
- - ecr:BatchCheckLayerAvailability
- - ecr:GetDownloadUrlForLayer
- - ecr:BatchGetImage
- - logs:CreateLogStream
- - logs:PutLogEvents
- Resource: '*'
- RoleECSExecution:
- Type: AWS::IAM::Role
- Properties:
- AssumeRolePolicyDocument:
- Version: "2012-10-17"
- Statement:
- -
- Effect: "Allow"
- Principal:
- Service:
- - "ecs-tasks.amazonaws.com"
- Action: "sts:AssumeRole"
- ManagedPolicyArns:
- - !Ref PolicyECSExecution
- RoleName: "RoleECSExecution"
- LogstashTaskDef:
- Type: AWS::ECS::TaskDefinition
- Properties:
- NetworkMode: awsvpc
- RequiresCompatibilities:
- - FARGATE
- Cpu: !Ref ContainerCPU
- Memory: !Ref ContainerMemory
- ExecutionRoleArn: !Ref RoleECSExecution
- TaskRoleArn: !Ref LogstashRolePutToS3
- ContainerDefinitions:
- -
- Name: !Ref LogstashContainerName
- Image: !Ref ImageUrl
- PortMappings:
- -
- ContainerPort: !Ref SyslogInputPort
- HostPort: !Ref SyslogInputPort
- LogConfiguration:
- LogDriver: awslogs
- Options:
- awslogs-region: !Ref AWS::Region
- awslogs-group: !Ref LogGroup
- awslogs-stream-prefix: ecs
- Environment:
- - Name: S3_BUCKET_BUCKETNAME
- Value: !Ref S3BucketName
- - Name: S3_BUCKET_CANNED_ACL
- Value: !Ref S3BucketCannedACL
- - Name: SYSLOG_INPUT_PORT
- Value: !Ref SyslogInputPort
- - Name: S3_BUCKET_REGION
- Value: !Ref S3BucketRegion
- LogstashContainerSecurityGroup:
- Type: AWS::EC2::SecurityGroup
- Properties:
- GroupDescription: !Ref LogstashContainerSecurityGroupDescription
- VpcId: !Ref LogstashVPC
- SecurityGroupIngress:
- - IpProtocol: tcp
- CidrIp: !Ref ContainerAllowInboundIPs
- FromPort: !Ref SyslogInputPort
- ToPort: !Ref SyslogInputPort
- - IpProtocol: tcp
- CidrIp: !Ref ContainerAllowInboundIPs
- FromPort: 80
- ToPort: 80
- LogstashService:
- Type: AWS::ECS::Service
- Properties:
- ServiceName: !Ref ServiceName
- Cluster: !Ref LogstashCluster
- TaskDefinition: !Ref LogstashTaskDef
- DesiredCount: !Ref DesiredCount
- LaunchType: FARGATE
- NetworkConfiguration:
- AwsvpcConfiguration:
- AssignPublicIp: !Ref AssignPublicIP
- Subnets:
- - !Ref LogstashSub1
- - !Ref LogstashSub2
- SecurityGroups:
- - !Ref LogstashContainerSecurityGroup
Advertisement
Add Comment
Please, Sign In to add comment